4.0类属性的封装

类属性封装后的赋值方法

package exercis;
class Student{
	private String name;
	private double score;
	//方法的重载
	public Student() {
	}
	public Student(String name,double score) {
	//两种赋值方法
		this.name=name;
		this.setScore(score);
	}
	
	
	public void setName(String name) {
		if(name=="") {
			System.out.println("请输入正确的姓名");
		}
		else {
			this.name = name;
		}
	}
	public String getName() {
		return name;
	}
	public void setScore(double score) {
		if(score<0||score>100) {
			System.out.println("请输入正确的成绩");
		}
		else {
			this.score = score;
		}
	}
	public double getScore() {
		return score;
	}
	public void info() {
		System.out.println("我的名字叫"+this.name+"成绩是"+this.score);
	}
}

public class Exercise{
	public static void main(String[] args) {
		Student s1=new Student();
		Student s2=new Student("张三",66);
		s1.setName("李四");
		s1.setScore(88);
		s1.info();
		s2.info();
	}
}

实例

package java_project;
class Student{
	private String name;
	private double score;
public Student(){
}
public Student(String name,double score)
{
	this.setName(name);
	this.setScore(score);
}

public void setName(String name){
	if(name=="")
		System.out.println("姓名不能为空!");
	else
		this.name=name;
}
public String getName()
{
	return this.name;
}
public void setScore(double score)  //设置时进行验证
{if(score<0 ||score>100)
	System.out.println("输入的成绩不合法,只能在0-100之间!");
else
	this.score=score;
}
public double getScore(){
return this.score;
}
public void info()
{
	System.out.println("这个人叫" + this.getName() +",她的成绩是:" + this.getScore());
}
}
/*定义测试类Test1,并创建两个Student对象,一个使用无参的构造方法,
然后调用第(2)步中定义的赋值方法给姓名和成绩赋值;另一个使用有参构造方法。
输出两个对象的基本信息 */
public class Test1 {
	public static void main(String[] args) {
	Student s1=new Student();
	Student s2=new Student("张三",99.9);
	s1.setName("李四");
	s1.setScore(88);
	s1.info();s2.info();
	}
}

或者

package qq;

class Student{
	private String name;
	private float score;
	//set get 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		if(name==""){
			System.out.println("name属性不能为空,结束程序");
			System.exit(0);
		}else{
			this.name = name;
		}
	}
	public float getScore() {
		return score;
	}
	public void setScore(float score) {
		if(score>100 || score<0){
			System.out.println("不合理的成绩,结束程序");
			System.exit(0);
		}else{
			this.score = score;
		}
		
	}
	//构造方法 
	public Student() {
		super();
	}
	public Student(String name, float score) {//改改改改
		super();
		this.setName(name);//this.name = name;
		this.setScore(score);//this.score = score;
	}
	//输出个人信息
	public void info(){
		System.out.println("我叫"+this.getName()+",我的成绩是:"+this.getScore()+"分");
	}	
}

public class Demo2 {

	public static void main(String[] args) {
		
		Student s1=new Student();
		s1.setName("张三");
		s1.setScore(55);
		Student s2=new Student("李四", 100);
		s1.info();
		s2.info();
		Student s3=new Student("", 100);
		s3.info();
		s3.info();
		Student s4=new Student("王五", -100);
		s4.info();
		s4.info();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值