J2SE学习笔记:J2SE重点难点,构造方法静态方法

来源:互联网 发布:软件代理怎么做 编辑:程序博客网 时间:2024/05/17 09:32

继承中子父类构造关系:

package cn.temp;class Person{  Person(){System.out.println("我会打印吗?") ;}   //如果没有这句,程序会报错,父类必须有空参构造Person(int i){System.out.println(i);}}class Worker extends Person{        Worker( int i){    //虽然传了参数,但是什么都没做。 //super();//这是自动访问父类中的空参数构造函数:Person(){},如果这里不写,那么就会自动加上一句隐式的super();  super(3);     }}public  class ConstructionDemo{public  static void main(String args[]){Worker worker  =  new Worker(1);  }}/** * 综上所述,父类必须有空参构造,子类构造里面都隐含了父类的super(); * 若是父类有有参构造,并且子类通过super关键词调用了,则父类空参构造就不再被子类调用 */

子父类静态方法关系:

package cn.temp;class TempFa {        public static void setAge(){System.out.println("father");}}public class Tempson extends TempFa{    public static void setAge(){System.out.println("son");}        public static void main(String[] args){            Tempson t = new Tempson();            t.setAge();    }    }/**eclipse在t.setAge()一行给了一行警告:The static method setAge() from the type Tempson should be accessed in a static way注意用词"should be accessed in a static way" should be 是祈使语气,即建议使用类名而不是对象调用。从理论上来说,static方法都属于类属性,应该是没有重写这一说,但是这样写也是可以的,但是eclipse会给警告。至于为什么@Overrider报错,这个就不清楚了,注解的底层没有研究过,总之代码行的通,但是理论上很别扭,不建议使用*/


原创粉丝点击