Java面向对象编程之封装、继承、多重继承
Java基础精华:
(1)类的封装:defs:类:变量和相关方法的集合,变量表明对象的状态,方法表明对象的行为。
特点:事物的内部实现细节隐藏起来;
对外提供一致的公共的接口--间接访问隐藏数据;
可维护性;
public class HelloWorld{
private String world="World";
public void say(){
System.out.println("Hello"+world);
}
public void main(String args[]){
HelloWorld helloworld = new HelloWorld();//对象的生成
helloworld.say();//调用对象的方法
System.out.println(helloworld.world);//调用对象的成员
}
}
(2)继承(inheritance)————抽象类
超类——子类
B extends A:B为子类,A为超类(Java中单一继承,c++是多重继承)
子类继承了超类定义的所有是实例变量和方法,MeanWhile,他可以定义自己的独特的变量和方法;
Examples:
class car{
int v;
void drive(){
System.out.println("Car: 速度"+v);
}
}
class Bus extends Car{
int p;//载客数量,special
void carry(){//special
System.out.println("Bus 载人"+p);
}
void sum(){//special
System.out.println("Bus 速度"+v);
System.out.println("");
}
}
():java多态是通过重写方法,虫子啊方法,覆盖方法实现的
覆盖——继承父类同名无参函数
重载——继承父类同名有参函数
重写——当前类的同名方法
5:抽象类与接口的用法:
必须被继承的类——abstract(可以有自己实现的方法和变量)
不能被继承的类——final(final 类【内部成员方法默认final 】,final 方法【可被继承,不可被修改】,final 变量【只能赋值一次】)
能多重继承的类——interface(A extends B:A仍是拓展的接口 ,A implements B,C,D :A是多重继承 的实体类)
接口方法可以有参数和返回类型,但不能有方法体;
接口中字段为static final 存储在静态存储区,本质上不属于接口;