1. 创建程序。
定义两个类:Account 和AccountTest类,体会类的封装性。
- Account 类要求具有属性:姓名(长度为2/3/4位),余额(必须>20),密码(必须为6位),如果不满足,则给出提示信息,并给默认值。
- 通过setXxx的方法给Account 的属性赋值。
- 在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;
}
}