问两个JAVA的问题。
1、例如我有个Student的类,并创建了个对象。 Student  s1;
   如何新建一个对象s2,使它的属性完全和s1想同但不是s1。即s1和s2不是同一个对象
2、JAVA有什么办法可以获得调用该方法的对象的吗?
    比如对象student有个方法setName();如何获得调用setName()方法的对象?
   谢谢大家!

解决方案 »

  1.   

    1,clone(),不过要自己在student类中实现。
    2,student中增加字段,把调用setName()方法的对象存起来。
      

  2.   

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

  3.   

    第二个问题:
    通过this关键字可以获得当前调用该方法的对象。
      

  4.   


    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
      

  5.   

    一定要implements Cloneable?
    如果我不能改动Student类的定义,而Student类本来就没有implements Cloneable怎么复制?
    应该有办法的吧!
      

  6.   

    比如说 String s1 = "sdfasdf";
    我要新建一个String 的对象s2,怎么复制到s2?
      

  7.   

    1. clone() 方法(一般深表复制要写自己的clone方法)。
    2. this 关键字。
      

  8.   

    public class Student
    {
        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();
        }
    }
      

  9.   

    不用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);
    }
      

  10.   

    如果是想获得调用者的“类”,倒是可以 new 一个 Throwable 来查找;如果是想获得调用者的“对象”……好像不行吧……
      

  11.   

    获得方法的对象?你是指获得setName()这个方法还是什么?
    如果是方法,则可通过类反射去获得(method方法)或者是上面说的this(本类中通过this调用)
      

  12.   


    如果要的是“被调用 setName() 方法的那个对象”,当然就是在 setName() 使用 this 了。但我感觉楼主要的是“调用 setName() 方法的那段程序的对象”,这个恐怕是没解的,甚至可能就不是从一个对象中调用的(比如从某个静态上下文中调用)。楼主赶紧出来澄清一下。
      

  13.   

    19楼说得对。
    我的意思是:
    比如说对象S1有个方法f();在f()的方法体内如何获得是哪个对象调用f()的?
    只是突发奇想,能实现吗?
      

  14.   

    我也是以初学者,不知道这样行不行啊//
    public class Test {
       public static void main(String []args) {
          Test t = new Test();
          System.out.println(t.f());
       }
        
       public Object f() {
          Object obj = this;
       }
    }
      

  15.   

    f方法中忘了返回了
    return obj;