java abstract抽象类学习

java抽象类
java abstract class

有时候,我们需要用到抽象类。比如我们想买水果,但是不确定买的是苹果还是香蕉
Sometimes, we want to abstract a class.For example, We want buy some fruits,but we are not sure we buy apple or pear.

我们抽象出一个类叫水果
Now we can abstract a class named Fruit

水果就作为了一个抽象类和一个父亲类
Fruit is a abstract class and it is a father class.

苹果和梨就作为子类
Apples and pears fruit is child class

注意:我们不能获得一个抽象类的对象 new Fruit() 会发生错误。
note: can not get a new abstract class new Fruit() is wrong

abstract father class

public abstract class Fruit {
	public String getColor(String color){
		return color;
	}

	public String getWeight(String weight){
		return weight;
	}
}



child class

public class Apple extends Fruit {

	public String getColor(String color){

		System.out.println(‘‘color is ‘‘ + color);

		return color;

	}



	public String getWeight(String weight){

		System.out.println(‘‘weight is ‘‘ + weight);

		return weight;

	}



	public static void main(String[] args){

		Fruit f = new Apple();

		//Fruit f = new Fruit();   can not get a new abstract class  new Fruit() is wrong

		f.getColor(‘‘red‘‘);

	}

}



首发于 http://java-er.com - http://java-er.com/blog/java-abstract/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值