关于抽象类-接口-多态的理解

想写一些关于多态,接口,抽象类方面的知识

假期的时候看《head first java》这里一直不太明白,今天想写C#笔记的时候,突然豁然开朗了

 

先不由分说的粘一段java代码(本来应该是C#笔记的,不过因为时间太紧就先不粘C#版的了,等有时间再补上)

public class Main {
	public static void main(String [] args){
		Main cur = new Main();
		cur.play();
	}
	
	public void play(){
		Animal dog = new Dog();
		Pet dog2 = new Dog();
		Animal cat = new Cat();
		((Dog)dog).walk();
		((Dog)dog2).sleep();
		
		Animal tiger = new Tiger();
		
		Animal crocodile = new Crocodile();
		
		Other_people_wirte_function(crocodile);
		Other_people_wirte_function(dog);
		Other_people_wirte_function(dog2);
		
	}
	
	public void Other_people_wirte_function(Animal animal){
		System.out.println("----------------------------------");
		System.out.println("I can do something with animal");
		System.out.println("But I did't care about which is Specific animal");
		System.out.println("Tiger, Cat, Dog, Crocodile, all is okey");
		
		animal.eat();
		animal.sleep();
		System.out.println("----------------------------------");
	}
	
	public void Other_people_wirte_function(Pet pet){
		System.out.println("----------------------------------");
		System.out.println("I can do something with pet");
		System.out.println("But I did't care about which is Specific Pet");
		System.out.println("Both Cat and Dog are okey");
		pet.walk();
		System.out.println("----------------------------------");
	}
	
	abstract public class Animal{
		abstract public void eat();
		abstract public void sleep();
		public void fight(){
			System.out.println("fight_EachOther");
		}
	}
	
	interface Pet{
		abstract public void walk();
	}
	
	public class Dog extends Animal implements Pet{
		public void eat(){
			System.out.println("D_eat");
		}
		public void sleep(){
			System.out.println("D_sleep");
		}
		public void walk(){
			System.out.println("D_walk");
		}
	}
	
	public class Cat extends Animal implements Pet{
		public void eat(){
			System.out.println("C_eat");
		}
		public void sleep(){
			System.out.println("C_sleep");
		}
		public void walk(){
			System.out.println("C_walk");
		}
	}
	
	public class Tiger extends Animal{
		public void eat(){
			System.out.println("T_eat");
		}
		public void sleep(){
			System.out.println("T_sleep");
		}
	}
	
	public class Crocodile extends Animal{
		public void eat(){
			System.out.println("C_eat");
		}
		public void sleep(){
			System.out.println("C_sleep");
		}
	}
}


输出结果是:




一.先说一下抽象类的作用:

1.动物类是抽象类,这里很明显,为啥呢?因为如果动物类中的实现了他子类所共有的功能,不需要他的子类去分别

实现,如不需要Tiger,Dog,等再去实现’fight‘功能了。

2.但对于某些功能比如sleep,eat,因为实现方式不一样,所以需要子类去重写这个函数,抽象类中只提供模板

3.实行多态,

Animal dog = new Dog();
用一个Animal类指针,去指一个具体化的Dog实例

public void Other_people_wirte_function(Animal animal){
		System.out.println("----------------------------------");
		System.out.println("I can do something with animal");
		System.out.println("But I did't care about which is Specific animal");
		System.out.println("Tiger, Cat, Dog, Crocodile, all is okey");
		
		animal.eat();
		animal.sleep();
		System.out.println("----------------------------------");
	}
对于别人写的函数,别人不需要知道你具体传的参数是什么。。

并且你可以在这个函数中调用你传入具体对象的方法。。。


这个例子中,你传入dog对象,在函数中调用的animal.eat(),其实就是调用的你的dog对象的方法


二。接口的作用

1.Pet是一个接口,如果你想让Dog,Cat类同时具有Pet的特性,就要用到接口的知识了

2.如果你在Animal类中添加pet方法,无疑是给Tiger,Crocodile类也强制添加了Pet特性

3.而如果在Dog,cat类中分别实现pet方法,又显得冗余,并且没有办法使用多态了

4.并且因为一个子类不能继承两个父类,所以接口是一个很好的替代品

5.那么接口的作用是什么呢?:多态,省去重复的代码

public void Other_people_wirte_function(Pet pet){
		System.out.println("----------------------------------");
		System.out.println("I can do something with pet");
		System.out.println("But I did't care about which is Specific Pet");
		System.out.println("Both Cat and Dog are okey");
		pet.walk();
		System.out.println("----------------------------------");
	}






同时用接口指向的dog实例,和用抽象类指向的dog实例

都可以被强制转化成Dog类指向的dog实例

Animal dog = new Dog();
		Pet dog2 = new Dog();
		((Dog)dog).walk();
		((Dog)dog2).sleep();

三。多态的作用

1.为了提高很多程序员一起工作的效率

2.保持程序的统一性,dog,cat,tiger实例,均可以被animal类的指针指引


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值