继承的复习

1、父类

package com.wyq.study;

public class Animal {
	private int age;
	public void setAge(int age){
		this.age = age;
	}
	public int getAge(){
		return age;
	}
	public Animal(){
		System.out.println("这里是无参构造");
	}
	public Animal(int age){
		this.age = age;
		System.out.println("这里是带参构造方法");
	}
	public void introduce(){
		System.out.println("这里是动物的自我介绍。");
	}
}

2、子类一

package com.wyq.study;

public class Bird extends Animal{
	private double weight;

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	public Bird(double weight) {
		super();
		this.weight = weight;
	}

	public Bird() {
		super();
	}
	public Bird(int age,double weight) {
		super(age);
		this.weight = weight;
	}

	@Override
	public void introduce() {
		
		super.introduce();
		System.out.println("这里是鸟类的自我介绍"+super.getAge()+"\t"+this.getWeight());
	}
	public void swimming(){
		System.out.println("鸟类会游泳");
	}
}

3、子类二

package com.wyq.study;

import javax.swing.plaf.synth.SynthScrollBarUI;

public class Fish extends Animal{
	private String color;

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public Fish(String color) {
		super();
		this.color = color;
		System.out.println("这里是鱼类的带参构造");
	}

	public Fish() {
		super();
	}
	@Override
	public void introduce() {
		System.out.println("这里是鱼类的自我解的接受啊"+this.color+"\t"+super.getAge());
		super.introduce();
	}
	public void fly(String place){
		System.out.println("鱼类在"+place+"飞,他的颜色是:"+this.color);
	}
	

}

4、测试类

package com.wyq.study;

public class TestAnimal {
	public static void main(String[] args) {
		Bird br = new Bird(12,23.6);
		br.introduce();
		br.swimming();
		Fish fs = new Fish("黄色");
		fs.fly("水里");
	}	
}

5、总结

1)先写父类,再写子类

2)子类继承父类

3)写公有的取值赋值方法get,set方法

4)写构造方法

5)写普通方法

6)方法的重新

7)在带参的构造方法中,使用this.属性和this.set属性,使用super调用父类的属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值