Java-继承与构造函数

class Animal
{
	public Animal(){
		System.out.println("Animal 的构造函数...");
	}
	
	public Animal(String x){
		System.out.println("Animal 带一个参数的构造函数"+ x);
	}
	
	public void eat(){
		System.out.println("Animal eat....");
	}
}

public class Cat extends Animal{
	
	public Cat(){
		System.out.println("Cat 的构造函数...");
	}
	
	public Cat(String x){
		System.out.println("Cat带一个参数的构造函数..."+ x);
	}
	
	public void eat(){
		System.out.println("Cat eat...");
	}
	
	public static void main(String[] args) {
		Animal one = new Animal();
		one.eat();
/*		Animal 的构造函数...
		Animal eat....*/
		
		Animal five = new Animal("haha");
		//Animal 带一个参数的构造函数haha
		
		Animal two = new Cat();   //父类声明,子类实现,行为表现是子类,子类构造函数若带参数,会调用父类无参数的构造方法
		two.eat();
		/*Animal 的构造函数...
		Cat 的构造函数...
		Cat eat...*/
		
		Animal four =new Cat("hehe");
		four.eat();
		/*Animal 的构造函数...
		Cat带一个参数的构造函数...hehe
		Cat eat...*/
		
		Cat three = new Cat();
		three.eat();
		/*Animal 的构造函数...
		Cat 的构造函数...
		Cat eat...*/
		
		Cat hehe = new Cat("memeda");
		/*Animal 的构造函数...
		Cat带一个参数的构造函数...memeda*/
	}
}



class baba
{
	public static void show()
	{
		System.out.println("我是爸爸类的静态方法...");
	}
	
	public void show(int x)
	{
		System.out.println("爸爸带一个参数"+x);
	}
}

public class erzi extends baba{

	public static void show()
	{
		System.out.println("我是儿子的重写静态方法...");
	}
	
	public void show(int x)
	{
		System.out.println("儿子带一个参数"+x);
	}
	
	public static void main(String[] args) {
		baba.show();
		erzi.show();
		System.out.println("--------------------------------");

		baba one = new baba();
		one.show();
		one.show(5);
		System.out.println("--------------------------------");

		baba two = new erzi();
		two.show();
		two.show(5);

	}
}

/*
 我是爸爸类的静态方法...
 我是儿子的重写静态方法...
--------------------------------
我是爸爸类的静态方法...
爸爸带一个参数5
--------------------------------
我是爸爸类的静态方法...
儿子带一个参数5
*/





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值