python
可以直接对实例化的属性进行赋值
class Test(): name = "小明" def __init__(self):{ //self.name = name; 不能调用, java如果设置了静态字段的话,是可以直接调用的, } a = Test() #小明 b = Test() #小明 c = Test() #小明 print(a.name) print(b.name) print(c.name) print("--------") a.name = "小红" //表示只给当前的实例添加了一个属性,name='小红',不影响其他的实例 print(a.name) #小红 print(b.name) #小明 print(c.name) #小明
java
public class Demo { public static void main(String[] args){ Test a = new Test("小明"); Test b = new Test("小红"); Test c = new Test("小花"); System.out.println(a.getInfo());//由于设置了private 所以不能直接调用a.name System.out.println(a.country);//中国 没有设置 private 所以可以 直接调用a.country System.out.println(b.country);//中国 System.out.println(c.country);//中国 a.country = "新中国"; //改变静态字段值,即改变了类字段,其他的实例,都用的是这个字段 System.out.println(a.country);//新中国 System.out.println(b.country);//新中国 System.out.println(c.country);//新中国 } } class Test{ private String name; static String country = "中国"; public Test(String name){ //构造方法 this.name = name; this.country = country; //可以去掉,没有任何效果 } public String getInfo(){ return this.name; //由于设置了私有字段(private),所以需要开辟接口,用来获取字段 } }
注意
如果属性为静态字段,构造方法中由对该静态字段重新赋值,修改的还是静态字段,并没有给实例创建新字段;
public class Demo { public static void main(String[] args){ Test a = new Test("小明","z"); Test b = new Test("小红","d"); Test c = new Test("小花","x"); System.out.println(a.country);//x //全部为最后一个创建实例时,设置的国家 System.out.println(b.country);//x System.out.println(c.country);//x a.country = "新中国";//直接将静态字段改变了 System.out.println(a.country);//新中国 System.out.println(b.country);//新中国 System.out.println(c.country);//新中国 } } class Test{ private String name; static String country = "中国"; public Test(String name,String country){ this.name = name; this.country = country; } }
如果需要每一个实例都有自己的国家,传入的字段不要设置为静态字段即可
public class Demo { public static void main(String[] args){ Test a = new Test("小明","z"); Test b = new Test("小红","d"); Test c = new Test("小花","x"); System.out.println(a.country);//z System.out.println(b.country);//d System.out.println(c.country);//x a.country = "新中国";//直接将静态字段改变了 System.out.println(a.country);//新中国 System.out.println(b.country);//d System.out.println(c.country);//x } } class Test{ private String name; String country = "中国"; public Test(String name,String country){ this.name = name; this.country = country; } public String getInfo(){ return this.name; //由于设置了私有字段(private),所以需要开辟接口,用来获取字段 } }
java可以向python一样直接给实例添加属性,前提先声明,不能是私有字段,[也最好不要是静态字段,不然多个实例,会共享这个字段]
public class Demo { public static void main(String[] args){ Test t = new Test(); t.name = "ddd"; System.out.println(t.name); } } class Test{ String name; }
此时的效果和python一样了
public class Demo { public static void main(String[] args){ Test a = new Test("小明"); Test b = new Test("小红"); Test c = new Test("小花"); System.out.println(a.country);//中国 System.out.println(b.country);//中国 System.out.println(c.country);//中国 a.country = "新中国"; //给当前的实例添加一个新属性,不影响其他的实例 System.out.println(a.country);//新中国 System.out.println(b.country);//中国 System.out.println(c.country);//中国 } } class Test{ private String name; String country = "中国"; public Test(String name){ this.name = name; } }