java和python对比----实例化的对象属性:
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对比----实例化的对象属性:的更多相关文章
- 对比java和python对比
		
对比java和python 对比java和python 2011年04月18日 1.难易度而言.python远远简单于java. 2.开发速度.Python远优于java 3.运行速度.java远优于 ...
 - Java中创建(实例化)对象的五种方式
		
Java中创建(实例化)对象的五种方式1.用new语句创建对象,这是最常见的创建对象的方法. 2.通过工厂方法返回对象,如:String str = String.valueOf(23); 3.运用反 ...
 - java和python对比
		
一:解释性和编译型 梳理 编译型:源代码经过编译直接变为二进制的机器语言,每次都可以直接重新运行不需要翻译.典型的就是c语言. 解释性:java和python都是解释型,源代码经过编译变为字节码文件, ...
 - python遍历并获取对象属性--dir(),__dict__,getattr,setattr
		
一.遍历对象的属性: 1.dir(obj) :返回对象的所以属性名称字符串列表(包括属性和方法). for attr in dir(obj): print(attr) 2.obj.__dict__:返 ...
 - java和python对比----1:
		
对计算来说: java 除法: 3/4 ==0; pyhton 除法: 3/4 ==0 3//4==0.75
 - java 通过反射获取和设置对象属性值
		
public static Object parseDate(Object object){ SimpleDateFormat sdf = new SimpleDateFormat("yyy ...
 - python 类实例化,修改属性值
		
class User(object): def __init__(self, first_name, last_name, login_attempts): self.first_name = fir ...
 - Mockito 中被 Mocked 的对象属性及方法的默认值
		
在 Java 测试中使用 Mockito 有段时日了,以前只是想当然的认为 Mock 的对象属性值和方法返回值都是依据同样的规则.基本类型是 0, 0.0, 或 false, 对象类型都是 null, ...
 - JavaScript 获取对象属性和方法
		
ShineJaie 原创整理,转载请注明出处. 一.获取对象属性和方法 Object.keys() 返回对象的可枚举属性和方法的名称数组. Object.getOwnPropertyNames() 返 ...
 
随机推荐
- Codeforces #662C Binary Table
			
听说这是一道$ Tourist$现场没出的题 Codeforces #662C 题意: 给定$n*m的 01$矩阵,可以任意反转一行/列($0$变$1$,$1$变$0$),求最少$ 1$的数量 $ n ...
 - 【blog】推荐一个博客系统后台管理模板 - pinghsu
			
pinghsu https://github.com/chakhsu/pinghsu
 - python正则表达式获取代理IP网站上的IP地址
			
import urllib.request import re def open_url(url): req = urllib.request.Request(url) req.add_header( ...
 - 新年第一个目标一张表盘串讲所有canves的知识点
			
我们的目标 首先是canves的坐标系统,基于浏览器的左上角为原点,x,y轴为正方向的坐标系统. 首先初始化,打标签 <canvas id="canvas" height=& ...
 - 如何生成添加前缀的顺序DIV
			
今天我们这边的需求是生产类似于 div1 div2 div3 这种的方式. filters: { pre: function (value) { return 'div' + value; } }, ...
 - for循环查找元素怎么跳出for循环
			
应用场景: 当我们通过for循环来循环对象或者数组时,当找到符合条件的数据时,想要跳出这个循环,不在执行循环继续往后面查找. 解决方法: for循环里面使用return没有效果,于是,我们回到最初控制 ...
 - linux shell 进阶篇、shell脚本编程-创建函数
			
使用函数 #!/bin/bash # testing the script function myfun { echo "This is an example of a function&q ...
 - Linux下的压缩和解压缩命令gzip/gunzip
			
作者:邓聪聪 Linux下的压缩和解压缩命令——gzip/gunzip yum -y install zip gzip (--安装压缩工具) gzip命令 gzip命令用来压缩文件.gzip是个使用广 ...
 - vc++高级班之窗口篇[4]---让程序只运行一个实例
			
大家都看过或者使用过类似只运行一个实例的程序,比如:QQ游戏.部分浏览器 等等! 让一个程序只运行一个实例的方法有多种,但是原理都类似,也就是在程序创建后,有窗口的程序在窗口创建前, 检查系统中是 ...
 - 无线桥接(WDS)如何设置?
			
一.WDS使用介绍 无线桥接(WDS)可以将多台无线路由器通过无线方式互联,从而将无线信号扩展放大.无线终端在移动过程中可以自动切换较好的信号,实现无线漫游. 本文指导将TL-WR740N当作副路由器 ...