八 java面向对象程序设计(构造方法)
八 java面向对象程序设计(构造方法)
/**
* 面向对象之二
* 构造方法(构造机):
*
* 1,构造方法的特征:
* 它具有与类相同的名称;
* 它不含返回值;
* 它不能在方法中用return语句返回一个值
* 注意:在构造方法里不含返回值的概念是不同于“void”的,在定义构造方法时加了“void”,结果这个方法就不再被自动调用了。
* 2,构造方法的作用:
* 当一个类的实例对象刚产生时,这个类的构造方法就会被自动调用,我们可以在这个方法中加入要完成初始化工作的代码。
* 也就是说,构造机最大的作用是为类成员初始化.
*/
class Constructor
{
private int i;
private String str;
//public Constructor(){}
//当我们不为创建的类添加构造方法时,系统会自己为我们添加一个默认的构造方法.
//但是,当我们已经为该类的加上构造方法后,那么系统将不再为我们自动创建构造方法了.
public Constructor()
{
System.out.println("default Constructor is calling");
i = -1;
str = "unknown";
}
public Constructor(int i)
{
System.out.println("one int type args Constructor is calling");
this.i = i;
str = "unknown";
}
public Constructor(int i,String str)
{
System.out.println("two args Constructor is calling");
this.i = i;
this.str = str;
}
public int getI(){return i;}
public String getStr(){return str;}
}
public class ConstructFunction {
public static void main(String[] args) {
Constructor ctor1 = new Constructor();
System.out.println("Constructor.i = " + ctor1.getI() + " " +
"Constructor.str = " + ctor1.getStr());
Constructor ctor2 = new Constructor(20);
System.out.println("Constructor.i = " + ctor2.getI() + " " +
"Constructor.str = " + ctor2.getStr());
Constructor ctor3 = new Constructor(30,"Co
相关文档:
一、编辑Java源文件
=============================================
Hello.java
=============================================
package test;
public class Hello
{
static
{
try
{
//此处即为本地方法所在链接库名
&n ......
定义:定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
适用性
许多相关的类仅仅是行为有异。“策略”提供了一种用多个行为中的一个行为来配置一个类的算法。
需要使用一个算法的不同变体。例如,你可能会定义一些反映不容的空间/时间权衡的算法。当这些变体实 ......
A flexible layout configurable with pattern string.
The goal of this class is to format
a LoggingEvent
and return the results as a String. The results depend on the conversion
pattern
.
The conversion pattern is closely related to the conversion pattern of the
printf function in C ......
//create a new Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.newDocument();
//add root Node
Element noteuser = d.createElement("note-users");
d.appendChil ......