Java面向对象(219)面向对象的特点2

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)

  1. 为什么要有继承?
    A. 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可。提高了代码的复用性。
    B. 继承的出现让类于类之间产生了关系,可以创建更为特殊的类型。
    C. 利于可维护性。
    此处的多个类称为子类,单独的这个类称为父类(基类或超类)。可以理解为:子类 is a 父类
  2. 类继承语法规则:public class Zoo extends Animal{ }
  3. 子类继承了父类,就继承了父类的方法和属性。
  4. 在子类中,可以使用父类中定义的方法和属性,也可以创建新的数据和方法。
  5. 在Java中,继承的关键字用的是 extends,即子类不是父类的子集,而是对父类的扩展。(子类更强大)
  6. 一个子类只能有一个直接父类,一个父类可以派生出多个子类。
  7. Java支持单继承,不允许多重继承。
  8. 例:
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());
	}
}
  1. 构造器:
    构造器默认先访问父类构造器再访问子类构造器;
    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

二. 面向对象特征之三:多态性

  1. 在Java中两种体现:方法的重载和重写 ; 对象的多态性(可以直接应用在抽象类和接口上)。
  2. Java引用变量有两个类型:编译时类型和运行时类型。编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定。
  3. 若编译时类型和运行时类型不一致时就出现了多态。
    Object pc2 = new PC();
    //父类 子类
    //编译时类型 运行时类型
    Computer c2 = new NotePad();
  4. 对象的多态—在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");
		}
	}
}

1314

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值