标题:构造函数是什么。。。
只看楼主
chenzhanpeng
Rank: 1
等 级:新手上路
帖 子:271
专家分:0
注 册:2007-1-5
 问题点数:0 回复次数:2 
构造函数是什么。。。
什么是构造函数,请举个例子说明下啊。。。
搜索更多相关主题的帖子: 函数 构造 
2007-05-24 21:43
a276202460
Rank: 2
等 级:新手上路
威 望:4
帖 子:392
专家分:1
注 册:2007-4-10
得分:0 

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


2007-05-24 21:52
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
得分: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()");
}


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-05-24 22:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-142274-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.307494 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved