第十次 构造方法-空参-有参-私有化 11.06

时间:2022-08-15 19:25:52
 1 package com.dengbaobao;
 2 public class Barry$June_person {
 3     public static void main(String args[]) {
 4         new Person();
 5         new Person();
 6         new Person();
 7         new Person();
 8         new Person();
 9         
10     }
11 }
12     class Person{
13         private String name;
14         private static  int count;
15         public Person() {
16             count++;
17             System.out.println("产生了" + count +"个实例化对象。 ");
18         }
19         public String getInfo() {
20             return "姓名: " + this.name;
21         }
22     }



第十次 构造方法-空参-有参-私有化 11.06






 1 package com.baidu;
 2 
 3 public class June$1 {
 4 
 5     public static void main(String[] args) {
 6         System.out.println(new Person().getInfo());
 7         System.out.println(new Person("A").getInfo());
 8         System.out.println(new Person("B").getInfo());
 9         System.out.println(new Person().getInfo());
10 
11     }
12 }
13     class Person{
14         private String name;
15         private static int count;
16         public Person() {
17             count++;
18             this.name = "NONAME - "+ count ;
19         }
20         public Person(String name) {
21             this.name=name;
22             
23         }
24         public String getInfo() {
25             return "姓名: "+ this.name;
26             }
27     }


第十次 构造方法-空参-有参-私有化 11.06








1 package com.baidu;
 2 public class June$2 {
 3     public static void main(String args[]) {
 4         Single s = null;
 5         s = Single.getInstance();
 6         s.print();
 7     }
 8 
 9 }
10     class Single{
11         private static Single instance = new Single();
12         private Single() {
13             
14         }
15         public static Single getInstance() {
16             return instance;
17         }
18         public void print() {
19             System.out.println("hello world!!!");
20         }
21     }



第十次 构造方法-空参-有参-私有化 11.06




 1 package com.bai;
 2 public class June$3 {
 3     public static void main(String[] args) {
 4         Persona per1 = new Persona("张三",30);
 5         Persona per2 = new Persona("李四",31);
 6         Persona per3 = new Persona("张五",32);
 7         System.out.println("---------------------信息修改之前-----------------------------");
 8         System.out.println(per1.getInfo());
 9         System.out.println(per1.getInfo());
10         System.out.println(per1.getInfo());
11         System.out.println("---------------------信息修改之后-----------------------------");
12         Persona.city = "B城";
13         System.out.println(per1.getInfo());
14         System.out.println(per1.getInfo());
15         System.out.println(per1.getInfo());
16     }
17 
18 }
19 class Persona{
20     private String name;
21     private int age;
22     static String city = "A城";
23     public Persona(String name,int age) {
24         this.name = name;
25         this.age = age;
26     }
27     public String getInfo() {
28         return " 姓名:"+ this.name + ",年龄:" + this.age + ", 城市: "+city;
29     }
30 }


第十次 构造方法-空参-有参-私有化 11.06






1 package com.bai;
 2 public class StaticDemo {
 3     public static void main(String args[]) {
 4         for (int x = 0; x < args.length; x++) {
 5             System.out.println(args[x] +" . ");
 6         }
 7 
 8     }
 9 
10 }



第十次 构造方法-空参-有参-私有化 11.06






1 package com.bai;
 2 public class Ehanshi {
 3         public static void main(String[] args) {
 4             Ehanshi s=Ehanshi.getSinglePerson();
 5             System.out.println(s.toString());//得到唯一的对象
 6         }
 7 
 8     private String name="The God";
 9     private int age=10000;
10     private static Ehanshi  person=new Ehanshi();//生成对象放在java静态池中,否则如果非静态在堆中,生命周期比类短
11     private Ehanshi() {};//构造方法私有化可以避免大量实例化对象而消耗内存
12     public static Ehanshi getSinglePerson() {//静态方法类外部可以通过类名.获取
13         return person;
14     }
15     @Override
16     public String toString() {
17         return "SinglePerson [name=" + name + ", age=" + age + "]";
18     }
19 
20 }




第十次 构造方法-空参-有参-私有化 11.06