正在加载图片...
class Sheep private string name; private float weight; private string color; public Sheep(string name, float weight, String color)t this name=name; this, weight=weight this color=color; public Sheep(sheep other) this. name=other name this weight=- other weight;拷贝构造函数 this color=other color: ∥…它方法 Sheep sheep= new Sheep(“ Xiaobai”,45,“ White”); Sheep sheep=new Sheep(sheep);Copy Constructor • 拷贝构造函数是指以类自身的引用类型作为 参数,在构造函数中将作为参数的对象的状 态(对象的域)赋值给新建的对象 • 在拷贝构造函数中可以直接访问作为参数的 对象的任何域 class Sheep{ private String name; private float weight; private String color; public Sheep(String name,float weight,String color){ this.name=name; this.weight=weight; this.color=color; } public Sheep(Sheep other){ this.name=other.name; this.weight=other.weight; this.color=other.color; } // ……其它方法 } 拷贝构造函数 Sheep sheepA=new Sheep(“XiaoBai”,45, “White”); Sheep sheepB=new Sheep(sheepA);
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有