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),所以需要开辟接口,用来获取字段 } }