一个简单java类的编写以及注意事项

1.类的名称要有意义

2.类中属性必须用private封装,必须有setter和getter方法

3.可以有多个构造方法,但必须包含无参数的构造方法

4.类中不允许有输出语句,所有内容获取必须必须返回

package asrfasf; //打包

class Address{    //简单Java类
	private String country;
	private String province;
	private String city;
	private String street;
	private String post;
	public Address(){}//无参数的构造函数
	public Address(String country,String province,String city,String street,String post){
		this.country=country;
		this.province=province;
		this.city=city;
		this.street=street;
		this.post=post;
	} 
	public void setCountry(String country){
		this.country=country;
	}
	public void setProvince(String province){
		this.province=province;
	}
	public void setCity(String city){
		this.city=city;
	}
	public void setStreet(String street){
		this.street=street;
	}
	public void setPost(String post){
		this.post=post;
	}
	public String getCountry(){
		return this.country;
	}
	public String getProvince(){
		return this.province;
	}
	public String getCity(){
		return this.city;
	}
	public String getStreet(){
		return this.street;
	}
	public String getPost(){
		return this.post;
	}
	public String getInfo(){    //返回数据
	return "国家:"+this.country+",省份:"+this.province+",城市:"+
				this.city+",街道:"+this.street+", 邮编:"+this.post;
    }
 }
public class Demo{ 
//Java程序入口
	public static void main(String[] args) {
		Address emp=new Address("中国","河北","石家庄","市里","000000");
		System.out.println(emp.getInfo());
	} 
}

猜你喜欢

转载自blog.csdn.net/qq_20799821/article/details/103339109