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;
}
}

  

java和python对比----实例化的对象属性:的更多相关文章

  1. 对比java和python对比

    对比java和python 对比java和python 2011年04月18日 1.难易度而言.python远远简单于java. 2.开发速度.Python远优于java 3.运行速度.java远优于 ...

  2. Java中创建(实例化)对象的五种方式

    Java中创建(实例化)对象的五种方式1.用new语句创建对象,这是最常见的创建对象的方法. 2.通过工厂方法返回对象,如:String str = String.valueOf(23); 3.运用反 ...

  3. java和python对比

    一:解释性和编译型 梳理 编译型:源代码经过编译直接变为二进制的机器语言,每次都可以直接重新运行不需要翻译.典型的就是c语言. 解释性:java和python都是解释型,源代码经过编译变为字节码文件, ...

  4. python遍历并获取对象属性--dir(),__dict__,getattr,setattr

    一.遍历对象的属性: 1.dir(obj) :返回对象的所以属性名称字符串列表(包括属性和方法). for attr in dir(obj): print(attr) 2.obj.__dict__:返 ...

  5. java和python对比----1:

    对计算来说: java 除法: 3/4 ==0; pyhton 除法: 3/4 ==0 3//4==0.75

  6. java 通过反射获取和设置对象属性值

    public static Object parseDate(Object object){ SimpleDateFormat sdf = new SimpleDateFormat("yyy ...

  7. python 类实例化,修改属性值

    class User(object): def __init__(self, first_name, last_name, login_attempts): self.first_name = fir ...

  8. Mockito 中被 Mocked 的对象属性及方法的默认值

    在 Java 测试中使用 Mockito 有段时日了,以前只是想当然的认为 Mock 的对象属性值和方法返回值都是依据同样的规则.基本类型是 0, 0.0, 或 false, 对象类型都是 null, ...

  9. JavaScript 获取对象属性和方法

    ShineJaie 原创整理,转载请注明出处. 一.获取对象属性和方法 Object.keys() 返回对象的可枚举属性和方法的名称数组. Object.getOwnPropertyNames() 返 ...

随机推荐

  1. .Net Core 配置文件appsettings

    1.配置文件为appsettings 在appsettings添加ConnectionStrings: { "Logging": { "IncludeScopes&quo ...

  2. zabbix系列~mysql进行监控

    一 简介:zabbix进行数据库监控 二 目的:采用percona进行插件式安装监控 三 安装 环境 zabbix_agent 步骤  yum -y install php php-mysql yum ...

  3. python - 代码缩进

    # -*- cording :utf-8 -*- # print absolute value of an integer a = 40 b = 1 if a >=50: print a els ...

  4. springboot多模块开发以及整合dubbo\zookeeper进行服务管理

    之前研究了springboot单工程的使用,参考git地址:https://github.com/qiao-zhi/springboot-ssm 下面研究springboot多模块开发的过程. 1.模 ...

  5. eclipse常用快捷键和插件

    1.快捷键 找实现类  ctrl +T 抽取為方法:alt+shift+M (Method) 方法返回值 ctrl+1 enter 2.在做Java项目的时候如何把第三方的jar包一起打包成jar文件 ...

  6. http://nancyfx.org + ASPNETCORE

    商务产品servicestack:  https://servicestack.net/ http://nancyfx.org  +  ASPNETCORE http://nancyfx.org    ...

  7. events.py 知识点记录

    1.__all__ __all__是一个字符串list,其他模块中使用from xxx import *的时候只能导入__all__列表里的内容 2.sys.version_info 获取版本号 im ...

  8. Windows PowerShell 入門(6)-関数編1

    この連載では.Microsoftが提供している新しいシェル.Windows Power Shellの使い方を解説します.今回は.関数の作成基礎と引数.戻り値.Switchパラメータについて説明します. ...

  9. boost::asio实现一个echo服务器

    以前使用ACE实现Server框架,但是觉得太笨重,决定采用boost.asio来写服务器程序: 1.服务器构建在linux上面:当然也可以在windows下运行 2.io部分采用非阻塞模式.业务逻辑 ...

  10. codeforces 412div.2

        A CodeForces 807A Is it rated?     B CodeForces 807B T-Shirt Hunt     C CodeForces 807C Success ...