1.多态
package Base_se.Base_012.b01;
public class Animal {
public void eat() {
System.out.println("动物吃东西");
}
}
package Base_se.Base_012.b01;
public class Cat extends Animal {
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
package Base_se.Base_012.b01;
public class AnimalDemo {
public static void main(String[] args) {
// 有父类引用指向子类对象
Animal a = new Cat();
}
}
2.多态中成员访问特点
package Base_se.Base_012.b02;
public class Animal {
public int age =40;
public void eat() {
System.out.println("动物吃东西");
}
}
package Base_se.Base_012.b02;
public class Cat extends Animal {
public int age = 20;
public int weight = 10;
@Override
public void eat() {
System.out.println("猫吃鱼");
}
public void play() {
System.out.println("玩游戏");
}
}
package Base_se.Base_012.b02;
public class AnimalDemo {
public static void main(String[] args) {
// 有父类引用指向子类对象
Animal a = new Cat();
// 访问成员变量:编译和运行都要看左边的
System.out.println(a.age); //40
// System.out.println(a.weight);
// 访问成员方法 编译左边 运行右边
a.eat();
// a.play();
}
}
3.多态的利弊
![](https://img-blog.csdnimg.cn/direct/beca6e54f726428b857ecd5fd19bd67a.png)
package Base_se.Base_012.b03;
public class Animal {
public void eat() {
System.out.println("动物吃东西");
}
}
package Base_se.Base_012.b03;
public class Cat extends Animal {
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
package Base_se.Base_012.b03;
public class Pig extends Animal{
@Override
public void eat() {
System.out.println("猪");
}
}
package Base_se.Base_012.b03;
public class Dog extends Animal{
@Override
public void eat() {
System.out.println("狗");
}
public void Look(){
System.out.println("看");
}
}
package Base_se.Base_012.b03;
public class AnimalOpti {
// public void useAnimal(Cat c){
// c.eat();
// }
//
// public void useAnimal(Dog d){
// d.eat();
// }
public void useAnimal(Animal a){
// Animal a = new Cat();
// Animal a = new Cat();
a.eat();
// a.look(); 不能访问的子类的特有方法
}
}
package Base_se.Base_012.b03;
public class AnimalDemo {
public static void main(String[] args) {
// 创建动物操作类的对象,调用方法
AnimalOpti ao = new AnimalOpti();
Cat cat = new Cat();
ao.useAnimal(cat);
Dog d = new Dog();
ao.useAnimal(d);
Pig pig = new Pig();
ao.useAnimal(pig);
}
}
4.多态中的转型
package Base_se.Base_012.b04;
public class Animal {
public void ext(){
System.out.println("动物吃东西");
}
}
package Base_se.Base_012.b04;
public class Cat extends Animal{
@Override
public void ext(){
System.out.println("猫吃鱼");
}
public void play(){
System.out.println("玩耍");
}
}
package Base_se.Base_012.b04;
public class AnimalDemo {
/*
* 向上转型:从子到父 父类引用指向子类对象
向下转型:从父到子 父类引用转为子类对象
* */
public static void main(String[] args) {
// 多态
Animal a = new Cat(); // 向上转型
a.ext();
// a.play();
// Cat cat = new Cat();
// cat.ext();
// cat.play();
// 向下转型
Cat c = (Cat) a; //强制转换为Cat 类型的
c.ext();
c.play();
}
}
5.多态内存图解
6.案例
package Base_se.Base_012.b05;
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("动物吃东西");
}
}
package Base_se.Base_012.b05;
public class Cat extends Animal{
public Cat() {
}
public Cat(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("猫");
}
}
package Base_se.Base_012.b05;
public class AnimalDemo {
public static void main(String[] args) {
// 创建猫类对象
Animal a = new Cat();
a.setName("加菲");
a.setAge(12);
System.out.println(a.getName()+","+a.getAge());
a.eat();
a = new Cat("加菲",13);
System.out.println(a.getName()+","+a.getAge());
a.eat();
}
}
7.抽象类
package Base_se.Base_012.b006;
public abstract class Animal {
// public void eat(){
// System.out.println("吃东西");
// }
// 抽象方法
// 抽象方法必须是抽象类 抽象类可以没有抽象方法 但是没什么意义
public abstract void eat();
public void sleep() {
System.out.println("睡觉");
}
}
package Base_se.Base_012.b006;
public class Cat extends Animal{
@Override
public void eat() {
System.out.println("猫");
}
}
package Base_se.Base_012.b006;
public class Dog extends Animal{
@Override
public void eat(){
System.out.println(111);
}
}
package Base_se.Base_012.b006;
public class AnimalDemo {
public static void main(String[] args) {
Animal a = new Cat();
a.eat();
a.sleep();
Animal d = new Dog();
d.eat();
}
}
8.抽象类的成员特点
package Base_se.Base_012.b07;
import javax.swing.*;
public abstract class Animal {
private int age = 20;
private final String city = "北京";
public Animal(int age) {
this.age = age;
}
public Animal() {
}
public void show() {
age = 40;
System.out.println(age);
System.out.println(city);
}
public abstract void eat();
}
package Base_se.Base_012.b07;
public class Cat extends Animal{
@Override
public void eat(){
System.out.println("猫");
}
}
package Base_se.Base_012.b07;
public class AnimalDemo {
public static void main(String[] args) {
Animal a = new Cat();
a.eat();
a.show();
}
}
9.猫和狗(抽象类版本)
package Base_se.Base_012.b08;
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();
}
package Base_se.Base_012.b08;
public class Cat extends Animal {
public Cat() {
}
public Cat(String name, int age) {
super(name, age);
}
@Override
public void eat(){
System.out.println("猫");
}
}
package Base_se.Base_012.b08;
public class Dog extends Animal {
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("狗");
}
}
package Base_se.Base_012.b08;
public class AnimalDemo{
public static void main(String[] args) {
Animal a = new Cat();
a.setName("猫1");
a.setAge(1);
System.out.println(a.getName()+","+a.getAge());
a = new Cat("mao2",11);
System.out.println(a.getName()+","+a.getAge());
}
}
10.接口
11.接口特点
package Base_se.Base_012.b09;
public interface Jumpping {
public abstract void jump();
}
package Base_se.Base_012.b09;
public class Cat implements Jumpping {
@Override
public void jump() {
System.out.println("猫");
}
}
package Base_se.Base_012.b09;
public abstract class Dog implements Jumpping{
// 此处必须重写接口中的方法
//或者变为抽象类
//Dog的子类以后继承DOg 还要重写Jumpping里面的方法
}
package Base_se.Base_012.b09;
public class JumppingDemo {
public static void main(String[] args) {
// Jumpping j = new Jumpping();
Jumpping j = new Cat();
j.jump();
}
}
12.接口成员特点
package Base_se.Base_012.b10;
public interface Inter {
public int num =10;
public final int n2 = 20;
public static final int n3 = 20;
// 都是常量 等价于int n3 = 20;
//接口里面无构造方法 主要是对行为的抽象
// 接口里面的成员方法只能是抽象的
public abstract void show();
void method();
}
package Base_se.Base_012.b10;
public class Interlmpl extends Object implements Inter{
public Interlmpl() {
super();
}
@Override
public void show(){
System.out.println("1");
}
@Override
public void method(){
System.out.println("2");
}
}
package Base_se.Base_012.b10;
public class InterfaceDemo {
public static void main(String[] args) {
Inter i = new Interlmpl();
// i.num = 111;
System.out.println(i.num);
// i.n2 = 222;
System.out.println(i.n2);
System.out.println(Inter.num);
i.show();
i.method();
}
}
13.猫和狗接口版本
package Base_se.Base_012.b11;
public interface Jumpping {
public abstract void jump();
}
package Base_se.Base_012.b11;
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();
}
package Base_se.Base_012.b11;
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("猫调高");
}
@Override
public void eat() {
System.out.println("猫吃饭");
}
}
package Base_se.Base_012.b11;
public class AnimalDemo {
public static void main(String[] args) {
Jumpping j = new Cat();
j.jump();
Animal a = new Cat();
a.setName("加菲");
a.setAge(12);
System.out.println(a.getName()+","+a.getAge());
a.eat();
((Cat) a).jump();
a = new Cat("加加",1);
System.out.println(a.getName()+","+a.getAge());
a.eat();
Cat c = new Cat();
c.setName("加菲");
c.setAge(12);
System.out.println(c.getName()+","+c.getAge());
c.eat();
c.jump();
}
}
14. 类和接口的关系
15.抽象类和接口的区别
举个例子 说明他们的区别:
如果我们想增加报警的方法,那么该放在那个地方呢 都给都放在抽象类和接口中 那么我们在进行实例化的时候 必须全部重写接口或者类中的方法 但是有的类却不能实现其他的方法 比如开关门
我们这样做:
这样这样:
16.案例
package Base_se.Base_012.b13;
public interface English {
public abstract void speak_english();
}
package Base_se.Base_012.b13;
public abstract class People {
private String name;
private int age;
public People() {
}
public People(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;
}
// 抽象的eat方法
public abstract void eat();
}
package Base_se.Base_012.b13;
public abstract class Teach extends People{
public Teach() {
}
public Teach(String name, int age) {
super(name, age);
}
public abstract void teach();
}
package Base_se.Base_012.b13;
public abstract class Sporter extends People{
public Sporter() {
}
public Sporter(String name, int age) {
super(name, age);
}
public abstract void study();
}
package Base_se.Base_012.b13;
public class basket_teach extends Teach{
public basket_teach() {
}
public basket_teach(String name, int age) {
super(name, age);
}
@Override
public void eat(){
System.out.println("篮球教练吃饭");
}
@Override
public void teach(){
System.out.println("篮球教练教学");
}
}
package Base_se.Base_012.b13;
public class Ping_teach extends Teach implements English{
public Ping_teach() {
}
public Ping_teach(String name, int age) {
super(name, age);
}
@Override
public void eat(){
System.out.println("乒乓球教练吃饭");
}
@Override
public void teach(){
System.out.println("乒乓球教练教学");
}
@Override
public void speak_english(){
System.out.println("乒乓球学习英语");
}
}
package Base_se.Base_012.b13;
public class Basket extends Sporter{
public Basket() {
}
public Basket(String name, int age) {
super(name, age);
}
@Override
public void eat(){
System.out.println("篮球运动员吃饭");
}
@Override
public void study(){
System.out.println("篮球运动员学习");
}
}
package Base_se.Base_012.b13;
public class Ping extends Sporter implements English{
public Ping() {
}
public Ping(String name, int age) {
super(name, age);
}
@Override
public void eat(){
System.out.println("乒乓球运动员吃饭");
}
@Override
public void study(){
System.out.println("乒乓球运动员学习");
}
@Override
public void speak_english(){
System.out.println("乒乓球运动员学英语");
}
}
package Base_se.Base_012.b13;
public class Demo {
public static void main(String[] args) {
Ping p = new Ping();
p.setName("甲");
p.setAge(12);
p.eat();
p.study();
p.speak_english();
System.out.println(p.getName() + "," + p.getAge());
System.out.println();
Basket b = new Basket("乙", 22);
b.eat();
b.study();
// b.speak_english();
System.out.println(p.getName() + "," + p.getAge());
}
}