正在回答
1回答
同学你好,1、直接写一个apple=5;是不符合语法规范的,需要在前面添加属性的类型,比如: int apple=5;
2、this需要在普通方法或者构造方法的方法体中使用,例如:
1 2 3 4 | public void play() { //调用本类中的apple属性 System.out.println( this .apple); } |
3、super同样需要在普通方法或者构造方法的方法体中使用,例如:
1 2 3 4 | public void play() { //调用父类中的apple属性 System.out.println( super .apple); } |
4、Apple2 apple2 = new Apple2(); apple2.apple=5;的内容需要在主方法中呐,而不是直接写在类中。
综上所述,代码如下:
1 2 3 | public class Apple { int apple= 3 ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class Apple2 extends Apple{ int apple= 5 ; //继承后,子类中的apple属性 public void play() { //直接调用apple属性 System.out.println(apple); //调用本类中的apple属性 System.out.println( this .apple); //调用父类中的apple属性 System.out.println( super .apple); } public static void main(String[] args) { Apple2 apple2 = new Apple2(); apple2.play(); apple2.apple= 6 ; } } |
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧