30天学会JAVA—练习题(2021韩顺平)——Day7

1. 创建程序。

定义两个类:Account 和AccountTest类,体会类的封装性。

  1. Account 类要求具有属性:姓名(长度为2/3/4位),余额(必须>20),密码(必须为6位),如果不满足,则给出提示信息,并给默认值。
  2. 通过setXxx的方法给Account 的属性赋值。
  3. 在AccountTest中测试。
public class Account {
   
	//属性
	private String name;
	private double remain;
	private String password;
	
	//封装
	public void setName(String name) {
   
		if(name.length() < 2 || name.length() >4){
   
			System.out.println("输入姓名错误,请重新输入!");
			this.name = "无名";
		}else{
   
			this.name = name;
		}

	}
	
	public String getName() {
   
		return name;
	}
	
	public void setRemain(double remain) {
   
		if(remain <= 20){
   
			System.out.println("余额错误,请重新输入!");
		}else{
   
			this.remain = remain;
		}
		
	}
	
	public double getRemain() {
   
		return remain;
	}
	
	public void setPassword(String password) {
   
		if(password.length() != 6){
   
			System.out.println("密码输入错误,请重新输入!");
			this.password = "000000";
		}else{
   
			this.password = password;
		}
		
	}	
	
	public String getPassword() {
   
		return password;
	}
	

	public void showInfo(){
   
		System.out.println("姓名:" + name + " 余额:" + remain + " 密码:" + password);
	}
}
public class AccountTest {
   
	public static void main(String[] args) {
   
		Account a = new Account();
		a.setName("abcd");
		a.setRemain(12.0);
		a.setPassword("1234");
		a.showInfo();
	}
}

2. 编写程序。

public class Computer {
   
	String cpu = "123";
	String memory = "32G";
	String disk = "16G";
	
	public void getDetails(){
   
		System.out.println("CPU:" + cpu + "内存:" + memory + "硬盘:" + disk);
	}
	public static void main(String[] args) {
   
		PC p = new PC();
		NotePad n = new NotePad();
		p.brand = "lenvo";
		n.color = "black";
		p.getDetails();
		System.out.println("品牌:" + p.brand);
		n.getDetails();
		System.out.println("演示:" + n.color);
	}
}

class PC extends Computer{
   
	String brand;
}

class NotePad extends Computer{
   
	String color;
}

3. 编写程序。

public class Person {
   
	private String name;
	private int age;
	
	public Person(String name, int age){
   
		this.name = name;
		this.age = age;
	}
		
	
	public String getName() {
   
		return name;
	}


	public void setName(String name) {
   
		this.name = name;
	}


	public int getAge() {
   
		return age;
	}


	public void setAge(int age) {
   
		this.age = age;
	}


	public void say(){
   
		System.out.println("我的名字是" + name + ",年龄为" + age); 
	}
	
	public static void main(String[] args) {
   
		Person p = new Person("张三", 24);
		p.say();
		Student s = new Student("李四", 10, "123", 60);
		s.say();
	}
}

class Student extends Person{
   
	private String id;
	private double score;
	
	public Student(String name, int age, String id, double score) {
   
		super(name, age);
		this.id = id;
		this.score = score;

	}

	public String getId() {
   
		return id;
	}

	public void setId(String id) {
   
		this.id = id;
	}

	public double getScore() {
   
		return score;
	}

	public void setScore(double score) {
   
		this.score = score;
	}
	
	public void say(){
   
// System.out.println("我的名字是" + getName() + ",年龄为" + getAge()
// +",我的id为" + id + ",分数为" + score);
		super.say();
		System.out.println(",我的id为" + id + ",分数为" + score);
	}
}

4. 重写equals方法。

判断两个Person对象的内容是否相等,如果两个Person对象的各个属性值都一样,则返回true,反之false。

public class Test03 {
   
	public static void main(String[] args) {
   
		Person person1 = new Person("ada", 12, '女');
		Person person2 = new Person("ada", 12, '女');
		
		System.out.println(person1.equals(person2));//false
	}
}

class Person {
   
	private String name;
	private int age;
	private char gender;
	
	@Override
	public boolean equals(Object obj) {
   
		//判断:如果比较的两个对象是同一个对象.则直接返回true
		if(this == obj){
   
			return true;
		}
		
		//类型判断
		if( obj instanceof Person){
    //是Person,才比较
			//进行 向下转型 (因为需要得到obj的各个属性)
			Person p = (Person) obj;
			return this.name.equals(p.name) && this.age== p.age && this.gender == p.gender;
		}
		//如果不是Person,则直接返回false
		return false;

	}
	
	
	public Person(String name, int age, char gender) {
   
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
	}

	public String getName() {
   
		return name;
	}

	public void setName(String name) {
   
		this.name = name;
	}

	public int getAge() {
   
		return age;
	}

	public void setAge(int age) {
   
		this.age = age;
	}

	public char getGender() {
   
		return gender;
	}

	public void setGender(char gender) {
   
		this.gender = gender;
	}		
}
全部评论

相关推荐

09-01 09:00
已编辑
四川旅游学院 运营
牛客55195891...:主要是专业不好,别的没毛病
牛客解忧铺
点赞 评论 收藏
分享
丿南烟丶:黑白模板吧,不要这样花哨的。 主要成就太空了,和获奖融在一起,写一两行就行了。 职业技能不要这样排,就传统的掌握精通什么什么然后举例补充的一些重要技术点。 自我介绍说实话也没啥用,可以删了。 把自己的两个项目方案细节补充上去,为什么这样设计,怎么设计,成果是什么按star法则来写 你要引导面试官来问你的技能和项目,你的获奖和自我介绍别人可能看都不看一眼或者不太在乎,重要的是展示你能干活的能力
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务