问两个JAVA的问题。
1、例如我有个Student的类,并创建了个对象。 Student s1;
如何新建一个对象s2,使它的属性完全和s1想同但不是s1。即s1和s2不是同一个对象
2、JAVA有什么办法可以获得调用该方法的对象的吗?
比如对象student有个方法setName();如何获得调用setName()方法的对象?
谢谢大家!
1、例如我有个Student的类,并创建了个对象。 Student s1;
如何新建一个对象s2,使它的属性完全和s1想同但不是s1。即s1和s2不是同一个对象
2、JAVA有什么办法可以获得调用该方法的对象的吗?
比如对象student有个方法setName();如何获得调用setName()方法的对象?
谢谢大家!
解决方案 »
- oracle 插入数据问题
- 关于String比较的问题,在线等.....
- 一个改变字体大小的applet的程序
- 为什么我按钮上的图片无法显示?
- 数据库的查询问题,包括数据库的方方面面,正常运行,结果不对!
- 腾讯正在举行活动,发放6位QQ号送分,http://vip.rl35.com/vip_qq/vip.htm?QQ=890890
- 请问Microsoft SDK for Java 4.0在那里有的下载阿?
- 如何将两个字节数组合并成一个字节数组
- 请问如何来运行.class文件,没有编译器。如何写.bat
- 我调用运营上的短信通道应该用什么请求啊
- 使用javah报错问题。
- jsp中utf-8和utf8是一回事么?为啥我的页面分别对待
2,student中增加字段,把调用setName()方法的对象存起来。
public class DrawPoint {
public static void main(String[] args) throws URISyntaxException, IOException {
Student s1 = new Student("xiaoli",20);
Student s2 = (Student)s1.clone();
System.out.println(s1.equals(s2));
}
}
class Student implements Cloneable{
private String name=null;
private int age=0;
Student(String pName,int pAge){
this.name = pName;
this.age = pAge;
}
public Object clone(){
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
通过this关键字可以获得当前调用该方法的对象。
public class DrawPoint {
public static void main(String[] args) throws URISyntaxException, IOException {
Student s1 = new Student("xiaoli",20);
Student s2 = (Student)s1.clone();
System.out.println(s1.equals(s2));
}
void test(){
DrawPoint d = this;//取得当前这个方法的引用
}
}
class Student implements Cloneable{
private String name=null;
private int age=0;
Student(String pName,int pAge){
this.name = pName;
this.age = pAge;
}
public Object clone(){
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
2
如果我不能改动Student类的定义,而Student类本来就没有implements Cloneable怎么复制?
应该有办法的吧!
我要新建一个String 的对象s2,怎么复制到s2?
2. this 关键字。
{
private String name;
private int age; public Student() {} public Student(String name,int age)
{
this.name = name;
this.age = age;
} public Student clone()
{
return new Student(name,age);
}
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
} public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
public class Test
{
public static void main(String[] args)
{
Student s1 = new Student("张三",25);
Student s2 = s1.clone();
}
}
class Student
{
private String name;
private int age;
public Student() {}
public Student(String name,int age)
{
this.name = name;
this.age = age;
}
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
} public static void main(String[] args){
Student stu1 = new Student("张三",20);
Student stu2 = new Student(stu1.getName,stu1.getAge);
}
如果是方法,则可通过类反射去获得(method方法)或者是上面说的this(本类中通过this调用)
如果要的是“被调用 setName() 方法的那个对象”,当然就是在 setName() 使用 this 了。但我感觉楼主要的是“调用 setName() 方法的那段程序的对象”,这个恐怕是没解的,甚至可能就不是从一个对象中调用的(比如从某个静态上下文中调用)。楼主赶紧出来澄清一下。
我的意思是:
比如说对象S1有个方法f();在f()的方法体内如何获得是哪个对象调用f()的?
只是突发奇想,能实现吗?
public class Test {
public static void main(String []args) {
Test t = new Test();
System.out.println(t.f());
}
public Object f() {
Object obj = this;
}
}
return obj;