牛客刷题记录第三天

package object;

/**
 * 1.子类构造器必须调用父类构造器
 * 2.静态方法要想使用非静态属性和方法,必须要创建对象,用对象.属性,对象.方法(),
 * 不能直接属性,方法()
 */
class Person {
    String name = "No name";

    public Person(String nm) {
        name = nm;
    }
}

class Employee extends Person {
    String empID = "0000";

    public Employee(String id) {
        super("zs");
        empID = id;
    }
}

public class TestEmp {
    int a = 1;

    void test() {
        System.out.println("test()");
    }

    static int b = 2;

    static void test1() {
        System.out.println("test2()");
    }

    public static void main(String args[]) {

        Employee e = new Employee("123");
        System.out.println(e.empID);
//        这两句会报错吗?
//        System.out.println(a);
//        test();

        //必须创建对象才能使用非静态属性和方法
        TestEmp testEmp = new TestEmp();
        System.out.println(testEmp.a);
        testEmp.test();

        //静态属性和方法可以直接使用,无需创建对象
        System.out.println(b);
        test1();
    }
}
posted @ 2023-10-05 12:15  yx1024  阅读(2)  评论(0编辑  收藏  举报