zoukankan      html  css  js  c++  java
  • Java作业八(2017-10-30)

    public class TAutoPerson {
    	public static void main(String args[]) {
    		          new Person();
    		          new Person();
    		          new Person();
    		          new Person();
    		          new Person();
    		          
    		     }
    		 }
    		     class Person{
    		         private String name;
    		         private static  int count;
    		         public Person() {
    		             count++;
    		             System.out.println("产生了" + count +"个实例化对象。 ");
    		         }
    		         public String getInfo() {
    		             return "姓名: " + this.name;
    		         }
    		     
     }
    

        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());
    		 
    		     }
    		 }
    		     class Person{
    		         private String name;
    		         private static int count;
    		         public Person() {
    		             count++;
    		             this.name = "NONAME - "+ count ;
    		         }
    		         public Person(String name) {
    		             this.name=name;
    		             
    		         }
    		        public String getInfo() {
    		             return "姓名: "+ this.name;
    		             }
    

    public class TAutoPerson {
    	public static void main(String args[]) {
    		          Single s = null;
    		          s = Single.getInstance();
    		          s.print();
    		      }
    		  
    		  }
    		     class Single{
    		         private static Single instance = new Single();
    		         private Single() {
    		             
    		         }
    		         public static Single getInstance() {
    		             return instance;
    		         }
    		         public void print() {
    		             System.out.println("hello world!!!");
    		         }
    }
    

         

    public class TAutoPerson {
    	public static void main(String[] args) {
    		          Persona per1 = new Persona("张三",30);
    		          Persona per2 = new Persona("李四",31);
    		          Persona per3 = new Persona("张五",32);
    		          System.out.println("---------------------信息修改之前-----------------------------");
    		          System.out.println(per1.getInfo());
    		          System.out.println(per1.getInfo());
    		         System.out.println(per1.getInfo());
    		         System.out.println("---------------------信息修改之后-----------------------------");
    		         Persona.city = "B城";
    		         System.out.println(per1.getInfo());
    		         System.out.println(per1.getInfo());
    		         System.out.println(per1.getInfo());
    		     }
    		 
    		 }
    		 class Persona{
    		     private String name;
    		     private int age;
    		     static String city = "A城";
    		     public Persona(String name,int age) {
    		         this.name = name;
    		         this.age = age;
    		     }
    		     public String getInfo() {
    		         return " 姓名:"+ this.name + ",年龄:" + this.age + ", 城市: "+city;
    		     }
    }
    

      

    public class TAutoPerson {
        public static void main(String[] args) {
        	TAutoPerson s=TAutoPerson.getSinglePerson();
                 System.out.println(s.toString());//得到唯一的对象
             }
     
         private String name="The God";
         private int age=10000;
         private static TAutoPerson  person=new TAutoPerson();//生成对象放在java静态池中,否则如果非静态在堆中,生命周期比类短
         private TAutoPerson() {};//构造方法私有化可以避免大量实例化对象而消耗内存
         public static TAutoPerson getSinglePerson() 
             return person;
         }
         @Override
         public String toString() {
             return "SinglePerson [name=" + name + ", age=" + age + "]";
         }
    }
    

      

  • 相关阅读:
    Java并发编程:同步容器
    poj 1961 Period
    html与JacaScript中的重要思想:预留后路、向后兼容、js分离
    SQL从头開始
    android帧动画,移动位置,缩放,改变透明度等动画解说
    COCOS学习笔记--Cocod2dx内存管理(三)-Coco2d-x内存执行原理
    构建基于Javascript的移动CMS——生成博客(二).路由
    Oracle 单表选择率
    刚接触Joomla,写一下瞎折腾的初感受~
    Android学习笔记之ProgressBar案例分析
  • 原文地址:https://www.cnblogs.com/chengxuyuanGM/p/7793390.html
Copyright © 2011-2022 走看看