218回顾笔记:面向对象—》封装
权限修饰符:
私有的:private :当前类中
默认权限 default :当前包
受保护的 protected:当前包,子类
共有的public :当前项目所有位置
提供get set 方法
public void setAge(int age){
//合法性判断
If(age>0){
this.age=age;
}
}
public void getAge(){
return this.age;
}
219:
一. 面向对象特征2:继承(extends)
- 为什么要有继承?
A. 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可。提高了代码的复用性。
B. 继承的出现让类于类之间产生了关系,可以创建更为特殊的类型。
C. 利于可维护性。
此处的多个类称为子类,单独的这个类称为父类(基类或超类)。可以理解为:子类 is a 父类 - 类继承语法规则:public class Zoo extends Animal{ }
- 子类继承了父类,就继承了父类的方法和属性。
- 在子类中,可以使用父类中定义的方法和属性,也可以创建新的数据和方法。
- 在Java中,继承的关键字用的是 extends,即子类不是父类的子集,而是对父类的扩展。(子类更强大)
- 一个子类只能有一个直接父类,一个父类可以派生出多个子类。
- Java支持单继承,不允许多重继承。
- 例:
public class Computer {
public Computer() {
// TODO Auto-generated constructor stub
}
public String name;
public int price;
public String hardDisk;
public String merory;
public String getDes() {
return name+" "+price+" "+hardDisk+" "+merory;
}
}
public class PC extends Computer{
public String post;
public void pcMethod() {
System.out.println("PC");
}
}
public class NotePad extends Computer{
public String noteFiled;
public void notepadMethod() {
System.out.println("notePad");
}
}
public class test {
public static void main(String[] args) {
PC pc1=new PC();
pc1.post="联想主机";
pc1.price=2000;
pc1.merory="8G";
NotePad notePad=new NotePad();
notePad.noteFiled="htyt";
notePad.name="苹果";
notePad.price=8000;
System.out.println(pc1.post);
System.out.println(pc1.getDes());
}
}
- 构造器:
构造器默认先访问父类构造器再访问子类构造器;
Super调用父类
This调用当前对象
子类中所有的构造器默认都会访问父类中空参数的构造器。
当父类中没有空参数的构造器时,子类的构造器必须通过this(参数列表)或者super(参数列表)语句指定调用本类或者父类中相应的构造器,且必须放在构造器的第一行。
如果子类构造器中既未显示调用父类或本类的构造器,且父类中又没有无参的构造器,则编译出错。
例:
public class Computer {
public Computer() {
System.out.println("computer");
}
}
public class PC extends Computer{
public String post;
public PC(){//访问子类构造函数之前,先访问父类的构造函数
super();//子类中每个构造函数内的第一行都有一句隐式的super()
System.out.println("PC");
}
}// computer PC
二. 面向对象特征之三:多态性
- 在Java中两种体现:方法的重载和重写 ; 对象的多态性(可以直接应用在抽象类和接口上)。
- Java引用变量有两个类型:编译时类型和运行时类型。编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定。
- 若编译时类型和运行时类型不一致时就出现了多态。
Object pc2 = new PC();
//父类 子类
//编译时类型 运行时类型
Computer c2 = new NotePad(); - 对象的多态—在Java中,子类的对象可以替代父类的对象使用
一个变量只能有一种确定的数据类型
一个引用类型变量可能指向(引用)多种不同类型的对象
public class Computer {
public String name;
public int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Computer() {
super();
}
public Computer(String name, int price) {
super();
this.name = name;
this.price = price;
}
public String say() {
return "Computer";
}
}
public class PC extends Computer{
public String post;
public PC(String post) {
super();
this.post = post;
}
public PC() {
super();
}
public PC(String name, int price) {
super(name, price);
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String say() {
return "PC";
}
}
public class test {
public static void main(String[] args) {
PC pc1 = new PC();
System.out.println(pc1.getPost());//正确
System.out.println(pc1.getName());
Computer pc2 = new PC();
//System.out.println(pc2.getPost());//错误
System.out.println(pc2.getName());
System.out.println(pc2.say());//虚方法调用
}
}
虚方法调用:先编译看父类里是否有,有 编译成功;
运行看子类里有没有,有 重写。
子类可以看作是特殊的父类,所以父类类型的引用可以指向子类的对象:向上转型。
5. instanceof 操作符:x instanceof A;检验x是否为类A的对象,返回值为Boolean型。
要求x所属的类于类A必须是子类和父类的关系,否则编译错误。
如果x属于类A的子类B,x instanceof A的值也为true。
public class test {
public static void main(String[] args) {
Computer c = new PC();
if(c instanceof NotePad){
System.out.println("false");
}else if(c instanceof Computer){//c instanceof PC
System.out.println("true c");
}else{
System.out.println("false");
}
}
}