多态:
同一个对象在不同时刻表现出来的不同形态。
前提:
- List item存在继承/实现关系
- 存在方法重写
- 父类引用指向子类对象
多态中成员访问特点
使用多态时,访问成员变量的特点:
编译看左边,运行看左边
使用多态时,访问成员方法的特点:
编译看左边,运行看右边public class Animal{ public int age=40; public void eat(){ System.out.println("动物吃东西"); } } public static Cat extends Animal{ oublic int age=20; public int weight =10; @Override public void eat(){ System.out.println("猫吃鱼"); } public void playGame(){ System.out.println("猫捉迷藏"); } } public class 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(); //成员变量编译执行都看左边,成员方法编译看左边,执行看右边 //因为成员方法有重写,成员变量没有 } }
public class Animal{ public void eat(){ System.out.println("动物吃东西" ); } } public class Cat extends Animal{ @Override public void eat() { System.out.println("猫吃鱼"); } } public class Dog extends Animal{ public void lookHome(){ System.out.println("狗看家"); } } public class AnimalOpeart{ /*public void useAnimal(Cat c){ c.eat(); } public void useAnimal(Dog d){ d.eat(); }*/ //把Animal a 传入这个方法 解决不同子类传入动作类的问题 public void useAnimal(Animal a){ a.eat(); } } public class AnimalDemo{ public static void main(String[] args) { AnimalOpeart ao=new AnimalOpeart(); Cat c=new Cat(); ao.useAnimal(c); c.eat(); Dog d=new Dog(); ao.useAnimal(d); d.lookHome(); } }
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.println("猫捉迷藏"); } } public class Dog extends Animal { @Override public void eat() { System.out.println("狗吃骨头"); } } public class AnimalDemo { public static void main(String[] args) { //向上转型 Animal a = new Cat(); a.eat(); //向下转型 Cat c = (Cat) a; c.eat(); c.playGame(); //向上转型 a = new Dog(); a.eat(); //向下转型 //classCastException 类型转换异常 // Cat cc=(Cat) a; // cc.eat(); //cc.playGame(); } }
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(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void eat(){ System.out.println("动物吃东西"); } } 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); } @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()); a.eat(); a=new Cat("大花猫",6); System.out.println(a.getName()+","+a.getAge()); a.eat(); System.out.println("--------------"); Animal d=new Dog(); d.setName("二哈"); d.setAge(6); System.out.println(d.getName()+","+d.getAge()); d.eat(); d=new Dog("大黄",5); System.out.println(d.getName()+","+d.getAge()); d.eat(); } }
抽象类:
当父类不知道该方法究竟该干啥,
就可以把这样的方法定义为抽象方法。
在java中,一个没有方法体的方法定义为抽象方法
,而类中有抽象方法,该类定义为抽象类。
```
**特点:**
抽象类必须有abstract关键字修饰。
抽象类不一定有抽象方法,有抽象方法的类一定是 抽象类。
抽象的子类:要么重写抽象类中的所有抽象方法。
要么是抽象类。
public abstract class Animal{
//抽象方法
public abstract void eat();
//抽象方法
public void sleep(){
System.out.println("睡觉");
}
}
public class Cat extends Animal{
//子类继承父类
// 父类是抽象类所以子类要重写父类所有抽象方法
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
public abstract class Dog extends Animal {
//抽象类不能被new
// 子类是个抽象类所以不用重写父类所有抽象方法
}
public class AnimalDemo {
public static void main(String[] args) {
Animal a =new Cat();
//向上转型
a.eat();
a.sleep();
}
}
public abstract class Animal {
//抽象类
//成员方法
private int age = 20;
private final String city = "北京";
//构造方法
public Animal() {
}
public Animal(int age) {
this.age = age;
}
//set/get方法
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
//定义show方法
public void show() {
age = 40;
System.out.println(age);
//被final修饰的变量回变成一个常量不能被再次赋值
// city="上海";
System.out.println(city);
}
//定义一个抽象方法
public abstract void eat();
}
public class Cat extends Animal{
//继承抽象父类 重写父类抽象方法
@Override
public void eat() {
}
}
public class AnimalDemo {
public static void main(String[] args) {
//向上转型 调用方法
Animal a=new Cat();
a.eat();
a.show();
}
}
猫狗案例升级版:
public abstract class Animal {
//定义一个抽象父类
//定义成员变量
private String name;
private int age;
//构造方法
public Animal() {
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
//set/get方法
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);
}
//重写抽象方法
@Override
public void eat() {
System.out.println("狗吃骨头");
}
}
public class AnimalDemo {
public static void main(String[] args) {
Animal a = new Cat();
//向上转型
//传入数据
a.setName("加菲猫");
a.setAge(6);
System.out.println(a.getName() + "," + a.getAge());
//调用eat方法
a.eat();
a = new Cat("加菲猫", 5);
System.out.println(a.getName() + "," + a.getAge());
a.eat();
Animal d = new Dog();
d.setName("二哈");
d.setAge(5);
System.out.println(d.getName() + "," + d.getAge());
d.eat();
d = new Dog("二哈", 5);
System.out.println(d.getName() + "," + d.getAge());
d.eat();
}
}
接口:
接口是一种公共的规范标准,只要符合规范标准,大家都可以用。java中的接口更多的体现在对行为的抽象。
接口的定义格式:
接口用interface修饰
public Interface 接口名{}
类实现接口用implements表示:
public class 类名 implements 接口名{}
接口成员的特点:
成员变量默认被:
public static final修饰。所以成员变量只能是常量。
构造方法:
没有, 因为接口是扩展功能没有具体存在。
成员方法:
只能是抽象方法。
默认修饰符:public abstarct
- List item接口不能被实例化,通过类对象实例化,接口多态。
- 接口的子类:重写接口中所以抽象方法 要么子类也是抽象类
- 一个类可以实现多个接口,接口与接口之间能多继承。
抽象类和接口的区别:
抽象类: 对事物的抽象
接口: 对行为的抽象;public interface Jumpping { //定义一个接口 //定义一个抽象方法 public abstract void jump(); } public class Cat implements Jumpping { //定义了一个类实现接口 重写接口中的所欲抽象方法 @Override public void jump() { System.out.println("猫可以跳高了"); } } public abstract class Dog implements Jumpping { //类实现接口 子类是个抽象类 所欲不用重写接口中所有抽象方法 } public class JumppingDemo { public static void main(String[] args) { // Jumpping j = new Jumpping(); //同抽象类来实现接口 Jumpping j = new Cat(); j.jump(); } }
```java
public interface Inter {
//变量都是常量默认被public static final 修饰
public int num=10;
public final int num2=20;
//相当于int num=20;
// public static final int num3=30;
int num3=30;
void show();
//相当于public void show(){};
public abstract void method();
}
public class InterImpl extends Object implements Inter{
//object是所有类的父类 类实现接口
//重写接口中的所有抽象方法
@Override
public void method() {
System.out.println("method");
}
@Override
public void 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);
}
}
public abstract class Animal {
//成员方法
private String name;
private int age;
//构造方法
public Animal() {
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
//set/get方法
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 interface Jumpping {
//定义接口 并且定义抽象方法
void jump();
}
public class Cat extends Animal implements Jumpping {
//子类继承父类实现接口
//构造方法
public Cat() {
}
public Cat(String name, int age) {
super(name, age);
}
//重写父类抽象方法
@Override
public void eat() {
System.out.println("猫吃鱼");
}
//重写接口抽象方法
@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=new Cat("加菲猫",5);
System.out.println(a.getName()+","+a.getAge());
a.eat();
Cat c=new Cat();
c.setName("加菲");
c.setAge(6);
System.out.println(c.getName()+","+c.getAge());
c.eat();
c.jump();
}
}
//抽象人类
public abstract class Person {
//成员方法
private String name;
private int age;
// 构造方法
public Person() {
}
public 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();
}
//说英语的接口
public interface SpeakEnglish {
public abstract void speak();
}
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 class PingPangCoach extends Coach 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("乒乓球教练说英语");
}
}
ublic class PingPangPlayer extends Player implements SpeakEnglish {
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 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 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("篮球运动员吃牛肉,和牛奶");
}
}
public class PersonDemo {
public static void main(String[] args) {
//创建对象
PingPangPlayer ppp = new PingPangPlayer();
ppp.setName("王浩");
ppp.setAge(30);
System.out.println(ppp.getName()+","+ppp.getAge());
ppp.eat();
ppp.study();
ppp.speak();
System.out.println("--------");
BasketballPlayer bp = new BasketballPlayer();
bp.setName("姚明");
bp.setAge(35);
System.out.println(bp.getName()+","+bp.getAge());
bp.eat();
bp.study();
System.out.println("------------");
BasketballCoach bc=new BasketballCoach();
bc.setName("杜洋");
bc.setAge(25);
System.out.println(bc.getName()+","+bc.getAge());
bc.eat();
bc.teach();
System.out.println("---------------");