面向对象编程练习题详解:Java代码实现与核心概念应用
21. 定义一个Person类
描述
Person类是面向对象编程中的一个基础类,用于表示一个人的基本信息。它包含以下属性和方法:
- 属性:
name(姓名):表示人的名字。age(年龄):表示人的年龄。gender(性别):表示人的性别。
- 方法:
introduce():用于打印出人的基本信息,例如“我的名字是XXX,我今年XX岁,我是XX性别。”
Java代码示例
class Person {
// 属性
private String name;
private int age;
private String gender;
// 构造方法
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
// Getter 和 Setter 方法
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
// introduce 方法
public void introduce() {
System.out.println("我的名字是" + name + ",我今年" + age + "岁,我是" + gender + "性别。");
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Person person = new Person("张三", 25, "男");
person.introduce();
}
}
22. 定义一个Student类
描述
Student类继承自Person类,用于表示学生的信息。它在Person类的基础上增加了以下属性和方法:
- 属性:
studentId(学号):表示学生的学号。
- 方法:
study():用于打印出学生正在学习的信息,例如“学生XXX正在学习。”
Java代码示例
class Student extends Person {
// 属性
private String studentId;
// 构造方法
public Student(String name, int age, String gender, String studentId) {
super(name, age, gender); // 调用父类的构造方法
this.studentId = studentId;
}
// Getter 和 Setter 方法
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
// study 方法
public void study() {
System.out.println("学生" + getName() + "正在学习。");
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Student student = new Student("李四", 20, "女", "20230101");
student.introduce();
student.study();
}
}
23. 定义一个Teacher类
描述
Teacher类继承自Person类,用于表示教师的信息。它在Person类的基础上增加了以下属性和方法:
- 属性:
title(职称):表示教师的职称。
- 方法:
teach():用于打印出教师正在教学的信息,例如“教师XXX正在教学。”
Java代码示例
class Teacher extends Person {
// 属性
private String title;
// 构造方法
public Teacher(String name, int age, String gender, String title) {
super(name, age, gender); // 调用父类的构造方法
this.title = title;
}
// Getter 和 Setter 方法
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
// teach 方法
public void teach() {
System.out.println("教师" + getName() + "正在教学。");
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Teacher teacher = new Teacher("王五", 35, "男", "教授");
teacher.introduce();
teacher.teach();
}
}
24. 定义一个Animal类
描述
Animal类是动物的基类,包含以下属性和方法:
- 属性:
name(名字):表示动物的名字。age(年龄):表示动物的年龄。
- 方法:
makeSound():用于打印出动物发出的声音,例如“动物XXX正在发出声音。”
Dog和Cat类继承自Animal类,并重写makeSound方法,以实现各自独特的声音。
Java代码示例
class Animal {
// 属性
private String name;
private int age;
// 构造方法
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
// Getter 和 Setter 方法
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;
}
// makeSound 方法
public void makeSound() {
System.out.println("动物" + name + "正在发出声音。");
}
}
class Dog extends Animal {
// 构造方法
public Dog(String name, int age) {
super(name, age);
}
// 重写 makeSound 方法
@Override
public void makeSound() {
System.out.println("狗" + getName() + "正在汪汪叫。");
}
}
class Cat extends Animal {
// 构造方法
public Cat(String name, int age) {
super(name, age);
}
// 重写 makeSound 方法
@Override
public void makeSound() {
System.out.println("猫" + getName() + "正在喵喵叫。");
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Animal animal = new Animal("动物", 5);
Dog dog = new Dog("旺财", 3);
Cat cat = new Cat("喵喵", 2);
animal.makeSound();
dog.makeSound();
cat.makeSound();
}
}
25. 定义一个Shape类
描述
Shape类是一个抽象类,用于表示几何形状。它包含以下抽象方法:
area():用于计算形状的面积。perimeter():用于计算形状的周长。
Circle和Rectangle类继承自Shape类,并实现area和perimeter方法。
Java代码示例
abstract class Shape {
// 抽象方法
public abstract double area();
public abstract double perimeter();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
@Override
public double perimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double area() {
return length * width;
}
@Override
public double perimeter() {
return 2 * (length + width);
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);
System.out.println("圆的面积:" + circle.area());
System.out.println("圆的周长:" + circle.perimeter());
System.out.println("矩形的面积:" + rectangle.area());
System.out.println("矩形的周长:" + rectangle.perimeter());
}
}
26. 定义一个BankAccount类
描述
BankAccount类用于表示银行账户,包含以下属性和方法:
- 属性:
balance(余额):表示账户的余额。
- 方法:
deposit(double amount):用于存款,增加账户余额。withdraw(double amount):用于取款,减少账户余额。如果余额不足,则打印提示信息。
Java代码示例
class BankAccount {
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("存款成功,当前余额为:" + balance);
} else {
System.out.println("存款金额必须大于0!");
}
}
public void withdraw(double amount) {
if (amount > 0) {
if (balance >= amount) {
balance -= amount;
System.out.println("取款成功,当前余额为:" + balance);
} else {
System.out.println("余额不足,无法取款!");
}
} else {
System.out.println("取款金额必须大于0!");
}
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000);
account.deposit(500);
account.withdraw(200);
account.withdraw(1500);
}
}
27. 定义一个Employee类
描述
Employee类用于表示员工的信息,包含以下属性和方法:
- 属性:
name(姓名):表示员工的名字。employeeId(工号):表示员工的工号。salary(工资):表示员工的工资。
- 方法:
calculateSalary():用于计算员工的工资。可以根据具体需求实现不同的工资计算逻辑。
Java代码示例
class Employee {
private String name;
private String employeeId;
private double salary;
public Employee(String name, String employeeId, double salary) {
this.name = name;
this.employeeId = employeeId;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void calculateSalary() {
System.out.println("员工" + name + "的工资为:" + salary);
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Employee employee = new Employee("赵六", "E001", 5000);
employee.calculateSalary();
}
}
28. 定义一个Car类
描述
Car类用于表示汽车的信息,包含以下属性和方法:
- 属性:
brand(品牌):表示汽车的品牌。model(型号):表示汽车的型号。speed(速度):表示汽车的当前速度。
- 方法:
accelerate():用于加速,增加汽车的速度。brake():用于刹车,减少汽车的速度。
Java代码示例
class Car {
private String brand;
private String model;
private int speed;
public Car(String brand, String model, int speed) {
this.brand = brand;
this.model = model;
this.speed = speed;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void accelerate() {
speed += 10;
System.out.println("汽车加速,当前速度为:" + speed);
}
public void brake() {
if (speed >= 10) {
speed -= 10;
System.out.println("汽车刹车,当前速度为:" + speed);
} else {
speed = 0;
System.out.println("汽车已经停止,当前速度为:" + speed);
}
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Car car = new Car("宝马", "X5", 0);
car.accelerate();
car.accelerate();
car.brake();
car.brake();
car.brake();
}
}
29. 定义一个Book类
描述
Book类用于表示书籍的信息,包含以下属性和方法:
- 属性:
title(书名):表示书籍的标题。author(作者):表示书籍的作者。price(价格):表示书籍的价格。
- 方法:
display():用于打印书籍的详细信息,例如“书名:XXX,作者:XXX,价格:XXX元。”
Java代码示例
class Book {
private String title;
private String author;
private double price;
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void display() {
System.out.println("书名:" + title + ",作者:" + author + ",价格:" + price + "元。");
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Book book = new Book("Java编程思想", "Bruce Eckel", 89.9);
book.display();
}
}
30. 定义一个Rectangle类
描述
Rectangle类用于表示矩形的信息,包含以下属性和方法:
- 属性:
length(长):表示矩形的长度。width(宽):表示矩形的宽度。
- 方法:
calculateArea():用于计算矩形的面积,公式为length * width。calculatePerimeter():用于计算矩形的周长,公式为2 * (length + width)。
Java代码示例
class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double calculateArea() {
return length * width;
}
public double calculatePerimeter() {
return 2 * (length + width);
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(4, 6);
System.out.println("矩形的面积为:" + rectangle.calculateArea());
System.out.println("矩形的周长为:" + rectangle.calculatePerimeter());
}
}
浙公网安备 33010602011771号