第八章
一、面向对象的特性:封装,继承,多态
1、封装
封装性是通过访问控制修饰符来限定应用程序内文件的访问级别的
访问级别:public:共有的
Protected:受保护
Default:默认
Private:私有的
例:public class Car{
//对属性的封装
private String brand;
private double length;
private double width;
private int seats=5;
public String getBrand(){
return brand;
}
public void setBrand(String brand){
This.brand=brand;
}
public String getLength(){
return length;
}
public void setLength(double brand){
This.length=length;
}
public String getWidth(){
return windth;
}
public void setWindth(double width){
This.width=width;
}
Public int getSeats(){
Return seats;}
}
例2:Output output=new Output();
2、继承
继承是通过extend来实现的
类A public class A{
Public void showA(){
System.out.println(a)
}
}
类b
Public class B extends A{
Public void showB(){
System.out.println(b)
}
Public static void main(String【】args){
B b=new B();
b.showA();
b.showB();
}
}
3、多态
(1)方法重载
(2)方法重写
4、Java中的类
(1)类
[修饰符] class类名[extends类名][implement 接口名]{
<类的成员变量说明>
<定义类的方法 >
}
(2)类的方法
[修饰符]返回类型 方法名 ([参数]){
方法体
}
被abstract修饰的方法是抽象类