10.30 私有化、静态 造自增人 练习,产生实例化对象,修改个人信息

1.package cn.dede.w;
class Person{
private String name;//私有化名字;
private static int count;//私有化、静态(能够调用);
public Person()
{
count++ ;//自增
this.name="恶心吧唧橡胶人-"+count;

}
public Person(String name)
{
this.name=name;
}
public String getInfo()
{
return"您的队友:"+this.name;
}
}
public class StaticA
{
public static void main(String args[])
{
System.out.println(new Person().getInfo());//转7
System.out.println(new Person("mlxg").getInfo());//转11
System.out.println(new Person("xiaohu").getInfo());
System.out.println(new Person().getInfo());
}
}

2.

package cn.dede.w;
class Single
{
private static Single instance=new Single();
private Single()
{

}
public static Single getInstance()
{
return instance;
}
public void print()
{
System.out.println("想要学仙术,哪能怕吃苦?");
}
}
public class StaticB
{
public static void main(String args[])
{
Single s = null;
s = Single.getInstance();
s.print();
}
}

3.

package cn.dede.w;
class Persona
{
private String name;
private static int count;
public Persona()
{
count++;
System.out.println("产生了"+count+"个实例化对象");
}
public String getInfo()
{
return"姓名"+this.name;
}
}
public class StaticD {
public static void main(String args[])
{
new Persona();
new Persona();
new Persona();
new Persona();
new Persona();
}
}

 4.

package cn.dede.w;
class Personc
{
private String name;
private int age;
static String city="稻城";
public Personc(String name,int age)
{
this.name=name;
this.age=age;
}
public String getInfo()
{
return"姓名:"+this.name+",年龄:"+this.age+ ",城市:"+ city;
}
}
public class StaticC
{
public static void main(String args[])
{
Personc per1=new Personc("MLXG",30);
Personc per2=new Personc("老大",31);
Personc per3=new Personc("老三",33);

System.out.println("信息修改前");
System.out.println(per1.getInfo());
System.out.println(per2.getInfo());
System.out.println(per3.getInfo());
System.out.println("信息修改后");
Personc.city="B城";
System.out.println(per1.getInfo());
System.out.println(per2.getInfo());
System.out.println(per3.getInfo());
}
}

 5.

package cn.dede.w;

public class StaticD {

    public static void main(String args[]) {
        // TODO 自动生成的方法存根
        for (int x=0;x<args.length;x++)
        {
            System.out.println(args[x]);
        }
    }

}
原文地址:https://www.cnblogs.com/dede-6/p/7763386.html