面向对象_方法重写的注意事项

64 篇文章 0 订阅
53 篇文章 0 订阅
/*
	方法重写的注意事项:
		A:父类中私有方法不能被重写
			因为父类私有方法子类根本就无法继承
		B:子类重写父类方法时,访问权限不能更低
			最好一致
		C:父类静态方法,子类也必须通过静态方法进行重写
			其实这个算不上方法重写,但是现像如此,至于为什么算不上方法重写,多态中理解
			
	子类重写父类的时候,最好声明一模一样
*/
class Father{
	//private void show(){}
	
	/*
	public void show(){
		System.out.println("show Father");
	}
	*/
	
	void show(){
		System.out.println("show Father");
	}
	
	/*
	public static void mathod(){
		
	}
	*/
	
	public void mathod(){
		
	}
}

class Son extends Father{
	//private void show(){}
	
	/*
	public void show(){
		System.out.println("show Son");
	}
	*/
	
	public void show(){
		System.out.println("show Son");
	}
	
	
	public static void mathod(){
		
	}
	
	/*
	public void mathod(){
		//Son 中的 mathod() 无法覆盖 Father 中的 mathod();被覆盖的方法为 static
		//public void mathod(){
        //1 错误
		
	}
	*/
	
	
}

class ExtendsDemo10{
	public static void main(String[] args){
		//创建对象
		Son s = new Son();
		s.show();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值