1. 如果x是一个类的对象且是可修改的,如List,Set等,用下面这种方式
Oneclass one = new Oneclass(new Integer(x));
Oneclass two = new Oneclass(new Integer(x));
2. 如果x是一个基本类型的变量如int, float, double, long, boolean,和不可变的类的对象如Integer, Double, String,用下面这种方式
int x = 50;
创建两个个实例:
Oneclass one = new Oneclass(x);
Oneclass two = new Oneclass(x);
Oneclass one = new Oneclass(new Integer(x));
Oneclass two = new Oneclass(new Integer(x));
2. 如果x是一个基本类型的变量如int, float, double, long, boolean,和不可变的类的对象如Integer, Double, String,用下面这种方式
int x = 50;
创建两个个实例:
Oneclass one = new Oneclass(x);
Oneclass two = new Oneclass(x);
解决方案 »
- 实现互动效果,给些建议
- 这种问题有点钻牛角尖,相信想把JAVA学好学透的人都会喜欢。
- 如何将字符串转换成类(字符串的名字跟类的名字一样)
- 把List<String> list中不存在Set<String> set的元素删除
- mouseDrag(Event evt,int mouseX,int mouseY)事件?
- 高分求助,JDOM build(InputStream)问题!
- 如何用paint和repaint方法???
- 问一个常用的问题?
- 请教几个简单问题哦。(applet加图片,jtree的拖放,panel加图标)
- applet连接数据库,写了一个policy文件,用appletviewer可以browse,但是如何在ie和nc中带那个policy文件?请各位帮忙一下...
- Component.this.getSize() 啥意思
- IOException异常出发后是否有固定的错误代码?
创建两个个实例:
Oneclass one = new Oneclass(x);
Oneclass two = new Oneclass(x);
当我在程序后面通过one.a = 100;修改了one的值,由于two也是引用x的地址创建的,所以two.a也会跟随变为100,尽管我没有直接修改two的值。不对吧?
除非你这样
Oneclass one=new Oneclass(x);
Oneclass two=one;后面的程序你修改one.a=100 two所指向跟one是同一个对象,所以two会跟着变动。
package pk;class Oneclass {
int a; Oneclass(int x1) {
a = x1;
}
}public class Inhiretance {
public static void main(String[] args) {
int x = 50;
Oneclass one = new Oneclass(x);
Oneclass two = new Oneclass(x);
two.a = 100;
System.out.println(one.a);
}
}
问题没还没组织好吧楼主