🎨 个人介绍
👉大家好,我是:知识的搬运工旺仔
👉认真分享技术,记录学习过程的点滴,如果我的分享能为你带来帮助,请支持我奥🍻
👉你的支持,是我每天更新的动力。
👉赞点:👍 留言:✍ 收藏:⭐
👉个人格言:想法一步一步的落实,才是你我前进最佳选择。
1 )什么是多态
2 )多态的前提
1 )成员访问特点
2 )代码实例
public class Animal {
public int age = 40;
public void eat() {
System.out.println("动物吃东西");
}
}
public class Cat extends Animal {
public int age = 20;
public int weight = 10;
@Override
public void eat() {
System.out.println("猫吃鱼");
}
public void playGame() {
System.out.prinln("猫捉鱼");
}
}
public static AnimalDemo {
public static void main(String[] args) {
//有父类引用指向子类对象
Animal a = new Cat();
System.out.println(a.age);
// 下面这个使用会报错,因为父类引用指向子类,只能使用父类的所有成员变量,子类新增的不能使用
System.out.println(a.weight);
a.eat();
// 这个方法会报错,因为父类引用指向子类,只能使用父类的所有方法,子类新增的不能使用
a.playGame();
}
}
1 )好处
2 )弊端
1 )向上转型
2 )向下转型
3 )代码演示
public class Animal {
public void eat() {
System.out.println("动物吃东西");
}
}
public class Cat extends Animal {
@Override
public void eat() {
System.out.println("猫吃鱼");
}
public void playGame() {
System.out.prinln("猫捉迷藏");
}
}
public class AnimalDemo {
public static void main(String[] args) {
// 上转型
Animal a = new Cat();
a.eat();
// 边下的方法会报错
a.palyGame();
//向下转型
Cat c = (Cat) a;
// 下边的d
a.eat();
a.palyGame();
}
}
1 )案例需求
请采用多态的思想实现猫和狗的案例,并在测试类中进行测试
2 )代码实现
public class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(String name , int age) {
this.name = name;
this,age = age;
}
public String getName() {
return name;
}
public void setName() {
this.name = name;
}
public int getAge() {
return age;
}
public void eat() {
this.age = age;
}
public void est() {
System.out.println("动物吃东西");
}
}
public class Cat extends Animal {
public Cat() {
}
public Cat(String name,int age) {
super(name,age);
}
@Override
public void eat() {
System.println.out("猫吃鱼");
}
}
public class Dog extends Animal {
public Dog() {
}
public Dog (String name,int age) {
super(name,age);
}
@Override
public void eat() {
System.out.println("狗吃骨头");
}
}
public class AnimalDemo {
public static void main(String[] args) {
//创建猫类对象进行测试
Animal a = new Cat();
a.setName("加菲");
a.setAge(5);
System.out.println(a.getName + "," + a.getAge());
e.eat();
a = new Cat("加菲",5);
System.out.println(a.getName() + "," + a.getAge());
a.eat();
}
}
1 )抽象类和抽象方法必须使用abstract 关键字修饰
// 抽象类的定义
poublic abstract class 类名 {
//抽象方法的定义
public abstract void eat();
}
2 )抽象类中不一定有抽象方法,有抽象方法的类一定是抽象类
3 )抽象类不能实例化
4 )抽象类的子类
1 )成员的特点
成员变量
构造方法
成员方法
2 )代码实例
public abstract class Animal {
private int age = 20;
private final String city = "北京";
public Aniaml() {}
public Animal(int age) {
this.age = age;
}
public void show() {
age = 40;
System.out.println
city = "上海"; // 这句话会报错,因为 city 是常量
System.out.println(city);
}
public abstract void eat();
}
public class Cat extends Animal {
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
public class AnimalDemo {
public static void main(String[] args) {
Animal a = new Cat();
a.eat();
a.show();
}
}
1 )案例需求
2 )代码实现
public abstract class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(String name , int age) {
this.name = name;
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 class Cat extends Animal {
public Cat() {
}
public Cat(String name,int age) {
super(name,age);
}
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
public class Dog extends Animal {
public Dog() {}
public Dog (String name , int age) {
super(name,age);
}
}
public class AnimalDemo {
publci static void main(String[] args) {
// 创建对象,按照多态的方式
Animal a = new Cat();
a.setName("加菲");
a.setAge(5);
System.out.println(a.getName + "," + a.getAge());
a.eat();
System.out.println("----------------");
a = new Cat("加菲",5);
System.out.println(a.getName() + "," + a.getAge());
a.eat();
}
}
1 )接口用关键字interface修饰
public interface 接口名 {}
2 )类实现接口用implements 表示
public class 类名 implements 接口名 {}
3 )接口 不能实例化
4 )接口的子类
1 )成员特点
成员变量:只能是常量,默认修饰符 :public static fianl
构造方法:没有,因为接口主要是拓展功能,而没有具体存在
成员方法:
2 )代码演示
public interface Inter {
public int num = 10;
public final int num2 =20;
int num3;
// 下面这行代码是会报错的,因为接口没有构造方法
public Inter() {}
public abstract void method();
}
public class InterImpl extends Object implements Inter {
public InterImpl() {
super();
}
@Override
public void method() {
System.out.println("method");
}
@Override
public void show() {
System.out.println("show");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Inter i = new InterImpl();
i.num = 20;
System.out.println(i.num);
i.num2 = 40; // 这行代码会报错
System.out.println(i.num2);
System.out.println(Inter.num);
}
}
1 )案例需求
2 )代码实现
public abstract class Animal {
private String name;
private int sge;
public Animal () {
}
public Animal(String name , int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getName() {
return age;
}
public void setName(int age) {
this.age = age;
}
public abstract void eat();
}
public interface Jumpping {
public abstract void jump();
}
public class Cat extends Animal implements JUmpping {
public Cat() {
}
public Cat (String name , int age) {
super(name,age);
}
@Override
public void jump() {
System.out.println("猫可以跳高了")
}
}
public class AnimalDemo {
public static void main(String[] args) {
//创建对象,调用方法
Jumpping j = new Cat();
j.jump();
System.out.println("----------");
Animal a = new Cat();
a.setName("加菲");
a.setAge(5);
System.out.println(a.getName + "," + a.getAge());
a.eat();
a.jump(); // 代码会报错
a = new Cat("加菲",5);
System.out.println(a.getName() + "," + a.getAge());
a.eat();
System.out.println("---------");
Cat c = new Cat();
c.setName("加菲");
c.setAge(5);
System.out.println(c.getName() + "," + c.getAge());
c.eat();
c.junp();
}
}
1 )类与类的关系
继承关系,只能单继承,但是可以多层继承
2 )类与接口的关系
实现关系,可以单实现,也可以多实现,还可以在继承一个类的同时实现多个接口
3 )接口与接口的关系
继承关系,可以单继承,也可以多继承
1 )成员区别
抽象类
接口
2 )关系区别
类与类
类与接口
接口与接口
3 )设计理念区别
抽象类
接口
我们现在有乒乓球运动员和篮球运动员,乒乓球教练和篮球教练
为了出国交流,跟乒乓球相关的人员都需要学习英语
请用所学知识分析,这个案例中有那些具体类,那些抽象类,那些接口,并用代码实现
public abstract class Person {
private String name;
private int age;
public Person() {}
public Person(String name , int age) {
this.name = name;
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 class Player extends Person {
public Player() {}
public Player(String name, int age) {
super(name,age);
}
public abstract void study();
}
public abstract class Coach extends Person {
public Coach() {}
public Coach(String name,int age) {
super(name,age);
}
public abstract void teach();
}
public interface SpeakEnglish {
public abstract void apeack();
}
public class BasketballCoach extends Coach {
public BasketballCoach() {}
public BasketballCoach(String name,int age) {
super(name,age);
}
@Override
public void teach() {
System.out.println("篮球教练教如何运球和投球");
}
@Override
public void eat() {
System.out.println("篮球教练吃羊肉,喝羊奶");
}
}
public class PingpangCoach extends implements SpeakEnglish {
public PingpangCoach () {}
public PingPangCoach(String name,int age) {
super(name,age);
}
@Override
public void teach() {
System.out.println("乒乓球教练教如何发球和接球");
}
@Override
public void eat() {
System.out.println("乒乓球教练吃小白菜,喝大米粥");
}
@Override
public void speak() {
System.out.println("乒乓球教练说英语");
}
}
public class PingpangPlayer extends Player implements SpeackEnglish {
public PingpangPlayer() {}
public PingpangPlayer(String name, int age) {
super(name,age);
}
@Override
public void study() {
System.out.println("乒乓球运动员学习如何发球和接球")
}
@Override
public void eat() {
System.out.println("乒乓球运动员吃大白菜,喝小米粥");
}
@Override
public void speak() {
System.out.println("乒乓球运动员说英语");
}
}
public class BasketballPlayer extends Player {
public BasketballPlayer() {}
public BasketballPlayer(String name,int age) {
super(name,age);
}
@Override
public void study() {
System.out.println("篮球运动员学习如何运球和投球")
}
@Override
public void eat() {
System.out.println("篮球远动员吃牛肉h");
}
}
🎈看完了不妨给我点个赞吧,👉你的支持,是我每天更新的动力…