1. 程式人生 > >10.30作業.

10.30作業.

pub out 天上 方法 extend idt -s 屬性 年齡

static屬性和方法如何調用:

技術分享

public class p {
    private String name;
    private int age;
    static String city ="A城";
    public p(String name,int age) {
        this.name=name;
        this.age=age;
    }
    public String getInfo() {
        return "姓名:"+this.name+",年齡:"+this.age+",城市:"+city;
    }
}
public
class qq { public static void main(String args[]) { p p1=new p("小明",30); p p2=new p("小白",21); p p3=new p("小紅",34); System.out.println("===分界線==="); System.out.println(p1.getInfo()); System.out.println(p2.getInfo()); System.out.println(p3.getInfo()); System.out.println(
"===分界線==="); p.city="B城"; System.out.println(p1.getInfo()); System.out.println(p2.getInfo()); System.out.println(p3.getInfo()); } }

構造方法私有化(餓漢式)

技術分享

public class ww {
    private static ww instance=new ww();
    private ww() {
        
    }
    public static
ww getInstance() { return instance; } public void print() { System.out.println("君不見"); System.out.println("黃河之水天上來"); System.out.println("奔流到海不復回"); System.out.println("詩仙李白"); } }
public class ee {
    public static void main(String args[]) {
        ww s=null;
        s=ww.getInstance();
        s.print();
    }

}

空參構造自動命名(有參構造)

技術分享

public class aa {
    private String name;
    private static int count;
    public aa() {
        count++;
        this.name="NONAME - "+count;
        
    }
    public aa(String name) {
        this.name=name;
    }
    public String getInfo() {
        return "姓名:" +this.name;
    }

}
public class zz {
    public static void main(String args[]) {
        System.out.println(new aa().getInfo());
        System.out.println(new aa("A").getInfo());
        System.out.println(new aa("B").getInfo());
        System.out.println(new aa().getInfo());
        
    }

}

空參構造輸出個數---static關鍵字

技術分享

public class ss {
    private String name;
    private static int count;
    public ss() {
        count++;
        System.out.println("地上有"+count+"朵花");
    }
    public String getInfo() {
        return "姓名:" +this.name;
    }

}
public class xx {
    public static void main(String args[]) {
        new ss();
        new ss();
        new ss();
        new ss();
    }

}

子父類

技術分享

abstract class TT {
    public TT() {
        this.print();
        }
    public abstract void print();
    }
class B extends TT {
    private int x=100;
    public B(int x) {
        this.x=x;
        }
    public void print() {
        System.out.println("x - "+x);
        }

    }
public class UU {
        public static void main(String args[]) {
            TT a=new B(10);
        }

}

10.30作業.