重读Think In Java -- Reusing Class

本文介绍了在编程中如何正确使用@override注解来避免方法重载与重写的混淆,并探讨了final关键字在变量、方法及类上的不同用途及其带来的限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 @override 引用符号

如在基类里有一方法为 void f(int i),你想在继承类中override这个方法,在打字时不小心把方法定义写成了void f(float i),这时,编译器会认为你要overload方法f(),而不是overrid。为避免此类错误,可以在继承类中把需要override的方法标记为@override,当出现上述错误时,编译器会给出警告。

 

Composition or Inheritance

当不确定使用Composition or Inheritance时,就问一句“Do I need to upcast?”

 

The final Keyword

  • using with data: The data can't be changed.The value of final variable can be set either at compile time or runtime.You can define  a final variable without setting a value,but u have to set value in the constructor. 
  • using with methods: means the method can't be overridden.
  • using with classes: means the class can't be inherited.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值