当前位置: 代码迷 >> Java相关 >> 构造函数是什么。。。
  详细解决方案

构造函数是什么。。。

热度:307   发布时间:2007-05-24 21:43:08.0
构造函数是什么。。。
什么是构造函数,请举个例子说明下啊。。。
搜索更多相关的解决方案: 函数  构造  

----------------解决方案--------------------------------------------------------

没个类都必须有构造方法 可是不一定是一个 也可以不写默认 区别就是构造函数的参数不同 都是一类名命名的 就像你在介绍你自己时 说明的自身特征的函数 初始化


----------------解决方案--------------------------------------------------------

class Meal {
Meal() { System.out.println("Meal()"); }//下面的每个类都有一个构造函数
}

class Bread {
Bread() { System.out.println("Bread()"); }
}

class Cheese {
Cheese() { System.out.println("Cheese()"); }
}

class Lettuce {
Lettuce() { System.out.println("Lettuce()"); }
}

class Lunch extends Meal {
Lunch() { System.out.println("Lunch()"); }
}

class PortableLunch extends Lunch {
PortableLunch() { System.out.println("PortableLunch()");}
}

public class Sandwich extends PortableLunch {
private static Test monitor = new Test();
private Bread b = new Bread();
private Cheese c = new Cheese();
private Lettuce l = new Lettuce();
public Sandwich() {
System.out.println("Sandwich()");
}


----------------解决方案--------------------------------------------------------
  相关解决方案