java创建对象的四种方式及为其属性赋值的两种方法

2017-04-29  本文已影响197人  i得深刻方得S

一.创建对象的四种方法

1.new

2.clone

3.反射

4.IO流

二.为属性赋值的两种方法

1传统的set,get方法为属性赋值

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

private String getSex() {

return sex;

}

private void setSex(String sex) {

this.sex = sex;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

2.//构造函数为属性赋值

public User() {

// TODO Auto-generated constructor stub

}

public User(String name, String sex, int age, String address) {

super();

this.name = name;

this.sex = sex;

this.age = age;

this.address = address;

}

上一篇下一篇

猜你喜欢

热点阅读