---------------------- 黑马程序员 Android培训、期待与您交流! ---------------------
1:final
(1)是一个关键字,表示的是最终的意思。 举例:毕老师装爹 :
class 毕爹 {
int age = 50;
public void teach() {
System.out.println("工商管理");
}
}
class 毕老师 extends毕爹 {
int age = 28;
public void teach() {
System.out.println("Java");
}
public void paly() {
System.out.println("看电影");
}
}
//别人来找他爹去讲课。他爹出差了。
老毕 B爹 = new 毕老师(); //传上爹的衣服,带上爹的眼睛,粘上小胡子 (向上转型)
System.out.println(B爹.age); //50 看起来要像爹的年龄
B爹.teach(); //工商管理
//B爹.paly(); 报错
//下课了,回家
老毕B = (毕老师)B爹; //脱下爹的衣服,去掉眼睛,拔掉胡子 (向下转型)
System.out.println(B.age); //28
B.teach(); //Java
B.paly(); //看电影
(4)抽象类的案例 :
举例 员工案例 :
abstract class Employee {
private String name;
private int pay;
public Employee (){}
public Employee (String name,int pay){
this.name=name;
this.pay=pay;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setPay(int pay){
this.pay=pay;
}
public int getPay(){
return pay;
}
//
public abstract void work();
}
class Coder extends Employee{
public Coder(){}
public Coder(String name, int pay){
super(name,pay);
}
public void work(){
System.out.println("根据任务,完成代码");
}
}
class Manager extends Employee{
public Manager(){
super("张三",31);
}
public Manager(String name,int pay){
super(name,pay);
}
private int bonus;
public void setBonus(int bonus){
this.bonus=bonus;
}
public int getBonus(){
return bonus;
}
public void work(){
System.out.println("判断需求,指定任务");
}
}
class EmployeeDemo {
public static void main(String[] args) {
Manager e = new Manager();
e.setName("牟经理");
e.setPay(12000);
e.setBonus(10000);
e.work();
System.out.println(e.getName()+"--月工资--"+e.getPay()+"-奖金-"+e.getBonus());
}
}
使用:具体。
//说英语
interface SpeakEnglish {
public abstract void speak();
}
//人抽象类
abstract class Person {
private String name;
private int age;
public Person() {}
public Person(String name,int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
//吃的抽象方法
public abstract void eat();
}
//运动员抽象类
abstract class Player extends Person {
public Player() {}
public Player(String name,int age) {
super(name,age);
}
//学习的抽象方法
public abstract void study();
}
//教练抽象类
abstract class Coach extends Person {
public Coach() {}
public Coach(String name,int age) {
super(name,age);
}
//教的抽象方法
public abstract void teach();
}
//具体的运动员类
//乒乓球运动员类
class PingPangPlayer extends Player implements SpeakEnglish {
public PingPangPlayer() {}
public PingPangPlayer(String name,int age) {
super(name,age);
}
public void speak() {
System.out.println("乒乓球运动员说英语");
}
public void eat() {
System.out.println("乒乓球运动员吃鸡蛋和粥");
}
public void study() {
System.out.println("乒乓球运动员学习如何发球和接球");
}
}
//篮球运动员类
class BasketBallPlayer extends Player {
public BasketBallPlayer() {}
public BasketBallPlayer(String name,int age) {
super(name,age);
}
public void eat() {
System.out.println("篮球运动员吃牛肉喝红牛");
}
public void study() {
System.out.println("篮球运动员学习如何带球和过人");
}
}
//具体的教练类
class PingPangCoach extends Coach implements SpeakEnglish {
public PingPangCoach() {}
public PingPangCoach(String name, int age) {
super(name,age);
}
public void speak() {
System.out.println("乒乓球教练说英语");
}
public void eat() {
System.out.println("乒乓球教练吃鸭蛋和鸭汤");
}
public void teach() {
System.out.println("乒乓球教练教如何发球和接球");
}
}
//篮球教练
class BasketBallCoach extends Coach {
public BasketBallCoach() {}
public BasketBallCoach(String name, int age) {
super(name,age);
}
public void eat() {
System.out.println("篮球教练吃驴肉火烧和白开水");
}
public void teach() {
System.out.println("篮球教练教如何带球和过人");
}
}
//测试类
class SportDemo {
public static void main(String[] args) {
//乒乓球运动员
PingPangPlayer ppp = new PingPangPlayer();
ppp.setName("王浩");
ppp.setAge(35);
System.out.println(ppp.getName()+"---"+ppp.getAge());
ppp.speak();
ppp.eat();
ppp.study();
System.out.println("------------------------------");
//篮球运动员
BasketBallPlayer bbp = new BasketBallPlayer();
bbp.setName("易建联");
bbp.setAge(30);
System.out.println(bbp.getName()+"---"+bbp.getAge());
bbp.eat();
bbp.study();
}
}
---------------------- 黑马程序员 Android培训、期待与您交流! ---------------------