java学习笔记

好久没看java了,稍微看了下了解了解,以下文字摘自java编程思想

 

 

java直接提供了abstract关键字,这个是C++没有的
javaa abstract method
抽象方法只有声明没有方法体,abstract void f( );
包含抽象方法的类叫做抽象类。
//C++中是包含纯虚函数的类叫做抽象类
If a class contains one or more abstract methods, the class itself must be qualified as abstract
如果想从抽象类继承并且创建新类型的对象,那么必须为抽象类中的抽象方法提供定义。


It’s possible to make a class abstract without including any abstract methods
抽象类无法实例化
p242

interface
接口
The interface keyword takes the concept of abstractness one step further
The interface keyword produces a completely abstract class, one that provides no implementation at all.
接口关键字产生的是完全抽象类,这个类没有提供任何实现。

接口interface关键字前加public,那么这个interface可以在不同package内调用。否则只能在与interface
相同的package内调用

An interface can also contain fields, but these are implicitly static and final

You can choose to explicitly declare the methods in an interface as public,
 but they are public even if you don’t say it
So when you implement an interface, the methods from the interface must be defined as public
当实现接口的时候,接口中的方法必须定义为public

every method in the interface is strictly a declaration
接口中的每个方法必须为声明形式。
接口同样也是不能实例化的

 

interface fruit{
	//the field defined in interface is static and final
	int count=1;
	public String getName();
	
}
class apple implements fruit{
	apple(String s){name=s;}
	public String getName(){return name;};
	private String name;
}
public class Testinterface  {
	public static void main(String[] args){
		fruit f;
		f=new apple("apple");
		System.out.println(f.getName());
		System.out.println(f.count);
		
	}
	
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值