Java基础班第九天(上)多态

三个内容 :多态、抽象类、接口 

第一节:讲解这个多态的概念

class  Demo1_Poly
{
	public static void main(String[] args) 
	{
		Animal c;
		c = new Cat();
		c.eat();
	}
	/*
	A:多态的概述
		事物存在的多种形式
	B:多态的前提
		1:要有继承关系
		2:要有方法重写
		3:要有父类引用指向子类对象
	*/
}
class Animal
{
	public void eat(){
		System.out.println("吃东西");
	}
}
class Cat extends Animal
{
	public void eat(){
		System.out.println("猫吃鱼");
	}
}

第二节 : 成员变量

class  Demo2_Poly
{
	public static void main(String[] args) 
	{
		father f;
		f = new son();
		son s;
		s = new son();

		System.out.println(f.num);
		System.out.println(s.num);

	}
}
class father
{
	int num = 10;
}
class son extends father
{
	int num = 20;
}

                                                                          图1 成员变量的图 编译看左边 运行看右边

第三节 成员方法 

     上图是成员方法的运行,在堆里面开辟了new空间以后,调用方法的时候,编译去找方法区的father,运行去找方法区的son,编译看左边,运行看右边 

第四节是静态方法 

第一节 概述,2 3 4分别介绍了 成员变量 成员方法 静态方法。除了成员方法 其他都是编译看左边,运行看左边。静态方法也是的原因是因为,静态方法也叫做类方法。所以相当于father.method()

class  Demo2_Poly
{
	public static void main(String[] args) 
	{
		father f;
		f = new son();
		son s;
		s = new son();

		System.out.println(f.num);
		System.out.println(s.num);
		s.print();
		s.method();

	}
}
class father
{
	int num = 10;
	public void print(){
		System.out.println("父类成员方法");
	}
	public static void method(){
		System.out.println("父类静态方法");
	}
}
class son extends father
{
	int num = 20;
	public void print(){
		System.out.println("子类成员方法");
	}
	public static void method(){
		System.out.println("父类静态方法");
	}
}

第五节 超人的故事

这个就是前面几节的一个具体的应用,救人的那个方法不能执行,是因为这是一个成员方法,编译看左边,运行看右边,而编译在父类中通过不了,所以错了

第六节 超人故事的改进

这个就是对上面的进行了改变

class Demo3_Flyman 
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		Person p;
		p = new SuperMan();
		System.out.println(p.name);
		//System.out.println(p.谈生意());
		p.谈生意();
		//p.救人();
		SuperMan sm = (SuperMan)p;
		sm.救人();
		int i = 1;  //这个是强制类型转换
		byte b = 2;
		b = (byte)i;
		System.out.println(b);
		i = b;
		System.out.println(i); //这个是自动类型转换,注意这里,b的值已经发生改变了
	}
}

class Person
{
	String name = "johb";
	public void 谈生意(){
		System.out.println("谈生意");
	}
}
class SuperMan extends Person
{
	String name = "superman";
	public void 谈生意(){
		System.out.println("谈三十万的工资");
	}
	public void 救人(){
		System.out.println("飞出去救人");
	}
}

 

 第七节  多态的好处和弊端 

这个代码比较难,自己多看几次,方法的抽取,只是说eat(),方法并不是创建对象,new对象的,可以用通过创建匿名对象。这个里面也有强制类型转换,其实这种引用类型也有强制类型转换和自动类型提升。基本数据类型中也会存在自动类型提升和强制类型转换。

class Demo4_Animal 
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		//Animal a1 = new Cat();
		//a1.eat();
		//第二步 抽取方法+匿名对象
		method(new Dog());
	}
	public static void method(Animal a){
		//Animal a = new Animal();  出现的问题1 自己多写了这句话  这句话用new dog()代替了
		//a.eat();
		if(a /*is*/ instanceof Cat){
			Cat c = (Cat)a;
			c.eat();
			c.catchMouse();
		}else{
			Dog d = (Dog)a;
			d.eat();
			d.lookHome();
		}
	}
}
class Animal
{
	public void eat(){
		System.out.println("动物吃饭");
	}
}
class Cat extends Animal
{
	public void eat(){
		System.out.println("猫吃鱼");
	}
	public void catchMouse(){
		System.out.println("抓老鼠");
	}
}
class Dog extends Animal
{
	public void eat(){
		System.out.println("狗吃肉");
	}
	public void lookHome(){
		System.out.println("看家");
	}
}

第八节 这个是两个练习 

我觉得第二个练习还是要多看看的,子类中有super.show();到了父类,是show2(),又要转到子类中,这个比较困难。

class test_poly {
	public static void main(String[] args) {
		A a = new B();
		a.show();
		B b = new C();
		b.show();
	}
}
class A
{
	public void show(){
		show2();
	}
	public void show2(){
		System.out.println("我");
	}
}
class B extends A
{
	public void show2(){
		System.out.println("爱");
	}
}
class C extends B
{
	public void show(){
		super.show();
	}
	public void show2(){
	
		System.out.println("你");
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_40079205/article/details/82949047