5:多态练习
钢铁侠案例
class Person{
private String name = "托尼斯塔克";
Person(){}
Person(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public void getMoney(){
System.out.println("开厂子");
}
}
class IronMan extends Person{
private String name = "钢铁侠";
IronMan(){}
IronMan(String name){
super(name);
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public void getMoney(){
System.out.println("合影");
}
public void Fly(){
System.out.println("Fly!");
}
}
class DuotaiTest{
public static void main(String[] args){
Person p = new IronMan();
//Person p = new Person();
System.out.println(p.getName());
p.getMoney();
IronMan im = (IronMan) p;
im.getMoney();
im.Fly();
}
}
榨汁机案例
class Juicer{
public void toJuice(Fruit f){
f.toJuice();
}
}
class Fruit{
public void toJuice(){
System.out.println("果汁");
}
}
class Apple extends Fruit{
public void toJuice(){
System.out.println("苹果汁");
}
}
class Banana extends Fruit{
public void toJuice(){
System.out.println("香蕉汁");
}
}
class JuicerTest{
public static void main(String[] args){
Juicer jc = new Juicer();
Fruit fa = new Apple();
fa.toJuice();
Fruit fb = new Banana();
fb.toJuice();
}
}
8:抽象类练习:
A:案例
具体事物:基础班老师,就业班老师
共性:姓名,年龄,讲课。
//抽象类 老师类
abstract class Teacher{
private String name;
private int age;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
//抽象方法
public abstract void teach();
}
class BaseTeacher extends Teacher{
public void teach(){
System.out.println("Java基础班老师"+getName()+"教Java基础");
}
}
class AndroidTeacher extends Teacher{
public void teach(){
System.out.println("Android就业班"+getAge()+"岁的老师教Android");
}
}
class TeacherTest{
public static void main(String[] args){
Teacher tb = new BaseTeacher();
tb.setName("小李");
tb.teach();
Teacher ta = new AndroidTeacher();
ta.setAge(30);
ta.teach();
}
}
B:案例
具体事物:猫,狗
共性:姓名,年龄,吃饭
//抽象类 动物类
abstract class Animal {
private String name;
private int age;
Animal (){}
Animal(int age){
this.age =age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public abstract void eat();
}
//定义子类
class Cat extends Animal{
public void eat(){
System.out.println(getName()+"吃鱼");
}
}
class Dog extends Animal{
public void eat(){
System.out.println(getName()+"吃肉");
}
}
Class AnimalTest{
public static void main(String[] args){
Animal ac = new Cat();
ac.setName("猫");
ac.eat();
Animal ad = new Dog();
ad.setName("狗");
ad.eat();
}
}
12:接口的练习
汪星人数数
interface Training {
public abstract void count();
}
class Dog{
public void eat(){
System.out.println("吃肉");
}
}
class WangXingRen extends Dog implements Training{
public void count(){
System.out.println("数数");
}
}
class WangXingRenShuShuTest{
public static void main(String[] args){
WangXingRen dw = new WangXingRen();
dw.eat();
dw.count();
}
}
14:案例:
猫狗案例,加入跳高的额外功能
猫狗分析:
A:案例演示
动物类:姓名,年龄,吃饭,睡觉。(抽象)
动物培训接口:跳高 (接口)
猫继承动物类
狗继承动物类
部分猫继承猫类并实现跳高接口
部分狗继承狗类并实现跳高接口
通过抽象类测试基本功能。
通过接口测试扩展功能。
只测试猫,狗的测试留给学生自己练习
//接口
interface Training{
public abstract void highJump();
}
//抽象类
abstract class Animal {
private String name;
private int age;
Animal (){}
Animal(int age){
this.age =age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public abstract void eat();
public abstract void sleep();
}
//子类
class Cat extends Animal{
public void eat(){
System.out.println(getName()+"吃鱼");
}
public void sleep(){
System.out.println(getAge()+"岁的猫晚上不睡觉");
}
}
class Dog extends Animal{
public void eat(){
System.out.println(getName()+"吃肉");
}
public void sleep(){
System.out.println(getAge()+"岁的狗晚上睡得香");
}
}
class SuperCat extends Cat implements Training{
public void highJump(){
System.out.println("跳得高");
}
}
class SuperDog extends Dog implements Training{
public void highJump(){
System.out.println("跳得远");
}
}
class AnimalHighJumpTest{
public static void main(String[] args){
SuperDog sd = new SuperDog();
sd.setName("汪星人");
sd.setAge(7);
sd.eat();
sd.sleep();
sd.highJump();
}
}
老师和学生案例,加入抽烟,打球的额外功能
//接口
interface Hobby{
public abstract void smoking();
public abstract void playBall();
}
//抽象类
abstract class Person{
private String name;
private int age;
Person (){}
Person(int age){
this.age =age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public abstract void eat();
public abstract void sleep();
}
//子类
class Student extends Person{
public void eat(){
System.out.println("吃米饭");
}
public void sleep(){
System.out.println("睡得晚");
}
}
class Teacher extends Person{
public void eat(){
System.out.println("吃馒头");
}
public void sleep(){
System.out.println("睡得早");
}
}
class SomeStudent extends Student implements Hobby{
public void smoking(){}
public void playBall(){
System.out.println("爱打球");
}
}
class SomeTeacher extends Teacher implements Hobby{
public void smoking(){
System.out.println("爱抽烟");
}
public void playBall(){}
}
class PersonHobbyTest{
public static void main(String[] args){
SomeTeacher st = new SomeTeacher();
st.setName("小李");
st.setAge(7);
System.out.println(st.getName()+"今年"+st.getAge()+"岁");
st.eat();
st.sleep();
st.smoking();
}
} 教练和运动员案例(学生分析然后讲解)
要求 : 三个属性
构造方法
get()/set()
其他成员方法 至少两个
//接口
interface Study{
public abstract void learnEng();
}
//抽象类 人类
abstract class Person{
private String name;
private int age;
Person(){}
Person(String name,int age){
this.name=name;
this.age=age;
}
//get/set
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
//特有方法
public abstract void eat(String food);
}
//抽象类 运动员类 继承人类
abstract class Player extends Person{
Player(){}
Player(String name,int age){
super(name,age);
}
public abstract void play();
}
//抽象类 教练类 继承人类
abstract class Coach extends Person{
Coach(){}
Coach(String name,int age){
super(name,age);
}
public abstract void teach();
}
//乒乓球运动员类 继承运动员类
class PPPlayer extends Player implements Study{
PPPlayer(){}
PPPlayer(String name,int age){
super(name,age);
}
public void eat(String food){
System.out.println(food+"就馒头.");
}
public void play(){
System.out.println("打乒乓球");
}
//重写接口中方法
public void learnEng(){
System.out.println("学好英语");
}
}
//篮球运动员类 继承运动员类
class Hoopster extends Player{
Hoopster(){}
Hoopster(String name,int age){
super(name,age);
}
public void eat(String food){
System.out.println(food+"就煎饼.");
}
public void play(){
System.out.println("打篮球");
}
}
//乒乓球教练类 继承教练类
class PPCoach extends Coach implements Study{
PPCoach(){}
PPCoach(String name,int age){
super(name,age);
}
public void eat(String food){
System.out.println(food+"就馅饼.");
}
public void teach(){
System.out.println("教乒乓球");
}
//重写接口中方法
public void learnEng(){
System.out.println("学好英语");
}
}
//篮球教练类 继承教练类
class BKBCoach extends Coach{
BKBCoach(){}
BKBCoach(String name,int age){
super(name,age);
}
public void eat(String food){
System.out.println(food+"就肉夹馍.");
}
public void teach(){
System.out.println("教篮球");
}
}
class PlayerTest{
public static void main(String[] args){
System.out.println();
PPPlayer ppp = new PPPlayer("张继科",30);
System.out.println("运动员"+ppp.getName()+",今年"+ppp.getAge()+"岁.");
ppp.eat("水果");
ppp.play();
ppp.learnEng();
}
}