1.
package cn.ch.w; public class Person { private String name; private static int count; public Person() { count++; System.out.println("产生了"+count+"个小蘑菇。"); } public String getInfo() { return "姓名:"+this.name; }
}
package cn.ch.w; public class StaticDemo { public static void main(String args[]) { new Person(); new Person(); new Person(); new Person(); new Person(); } }
2.
package cn.ch.w; public class Person { private String name; private static int count; public Person() { count++; this.name="xixixi-"+count; } public Person(String name) { this.name=name; } public String getInfo() { return"你谁?"+this.name; } }
package cn.ch.w; public class StaticDemo { public static void main(String args[]) { System.out.println(new Person().getInfo()); System.out.println(new Person("A").getInfo()); System.out.println(new Person("B").getInfo()); System.out.println(new Person().getInfo()); } }
3.
package cn.ch.w; public class Single { private static Single instance=new Single(); private Single() { } public static Single getInstance() { return instance; } public void print() { System.out.println("你喜欢吃蘑菇吗!!"); } }
package cn.ch.w; public class SingleDemo { public static void main(String args[]) { Single s=null; s = Single.getInstance(); s.print(); } }
4.
package cn.ch.w; public class Person { private String name; private int age; static String city="捷克共和国"; public Person(String name,int age) { this.name=name; this.age=age; } public String getInfo() { return "小蘑菇你叫啥?"+this.name+",多大啦:"+this.age+",住在哪里啊"+city; } }
package cn.ch.w; public class StaticDemo { public static void main(String arges[]) { Person per1=new Person("金针菇",21); Person per2=new Person("杏鲍菇",22); Person per3=new Person("茶树菇",23); System.out.println("------隐姓埋名前------"); System.out.println(per1.getInfo()); System.out.println(per2.getInfo()); System.out.println(per3.getInfo()); System.out.println("------隐姓埋名后------"); Person.city="中华人名共和国"; System.out.println(per1.getInfo()5.); System.out.println(per2.getInfo()); System.out.println(per3.getInfo()); } }
5.
package cn.ch.w; public class A { public A() { this.print(); } public void print() { } } class B extends A{ private int x=100; public B(int x){ this.x=x; } public void printf() { System.out.println("x= "+x); } }
package cn.ch.w; public class TestJava { public static void main(String args[]) { A a=new B(10); } }