Java基础编程500题——面向对象

时间:2025-03-28 20:07:19

 ???? 该系列属于【Java基础编程500题】专栏,如您需查看Java基础的其他相关题目,请您点击左边的连接

目录

1. 定义一个学生类(Student),包含姓名(name)和年龄(age)属性,并实现一个打印学生信息的方法。

2. 定义一个矩形类(Rectangle),包含长(length)和宽(width)属性,并实现计算面积和周长的方法。

3. 定义一个三角形类(Triangle),包含三个边长(a, b, c)属性,并实现一个判断是否为等边三角形的方法。

4. 定义一个银行账户类(BankAccount),包含账户余额(balance)属性,实现存款(deposit)和取款(withdraw)方法。

5. 定义一个动物类(Animal)和一个猫类(Cat),猫类继承动物类,并实现一个叫声方法。

6. 定义一个接口Flyable,包含一个飞行方法fly(),然后定义一个鸟类(Bird)实现该接口。

7. 定义一个抽象类Shape,包含一个计算面积的方法getArea(),然后定义一个圆形类(Circle)继承该抽象类,并实现计算面积的方法。

8. 定义一个接口可食用(Edible),包含一个方法isEdible(),然后定义一个水果类(Fruit)实现该接口。

9. 定义一个接口可充电(Chargeable),包含一个方法charge(),然后定义一个手机类(SmartPhone)实现该接口。

10. 创建一个Shape工具类,并定义一个计算面积的方法,重载该方法以计算圆和矩形的面积。

11. 定义一个用户类(User),包含用户名(username)和密码(password)属性,提供静态方法验证密码是否有效(长度至少为6),并提供getter和setter方法。

12. 定义一个图书类(Book),包含书名(title)和作者(author)属性,提供一个静态方法来打印所有图书Book[]的信息,并提供getter和setter方法。

13. 定义一个学生类(Student),包含姓名(name)和成绩(score)属性,提供一个静态方法计算所有学生的平均成绩,并提供getter和setter方法。

14. 定义一个学生类(Student),包含姓名(name)和成绩(score)属性。提供一个静态方法来找出最高分的学生,并提供getter和setter方法。

15. 写一个简单的String工具类。提供几个基本的方法,检查字符串是否为空、首字母大写、反转字符串。

16. 定义一个员工类Employee,包含姓名和年龄属性,以及一个显示信息的方法showInfo()。再定义两个子类Manager和Developer,分别重写showInfo()方法。在Main方法中,创建Manager和Developer对象,并通过向上转型为Employee类型,调用showInfo()方法。

17. 定义一个支付类Payment,包含一个抽象方法pay()。再定义两个子类CreditCardPayment和CashPayment,分别实现pay()方法。在Main方法中,创建CreditCardPayment和CashPayment对象,并通过向上转型为Payment类型,调用pay()方法。

18. 定义一个形状接口Shape,包含一个方法draw()。再定义两个实现类CircleShape和SquareShape,分别实现draw()方法。在Main方法中,创建CircleShape和SquareShape对象,并通过向上转型为Shape类型,调用draw()方法。

19. 定义一个父类Person和一个子类Student,都有introduce方法介绍自己,使用向上转型和向下转型调用introduce。

20. 定义一个Person基类和子类Administrator、Student和Teacher。每个子类都应重写一个show方法来展示各自类型的信息。Main方法中创建这些类的实例,并调用一个register方法来展示每个人的信息。register方法应接受一个Person类型的参数


   ✨✨  返回题目目录 ✨ ✨ 

Java基础编程500题


1. 定义一个学生类(Student),包含姓名(name)和年龄(age)属性,并实现一个打印学生信息的方法。

class Student {
    String name;
    int age;

    public Student(String name, int age) {
         = name;
         = age;
    }

    public void printInfo() {
        ("学生姓名:" + name + ",年龄:" + age);
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student("张三", 20);
        ();
    }
}

2. 定义一个矩形类(Rectangle),包含长(length)和宽(width)属性,并实现计算面积和周长的方法。

class Rectangle {
    double length;
    double width;

    public Rectangle(double length, double width) {
         = length;
         = 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(5, 3);
        ("矩形面积:" + ());
        ("矩形周长:" + ());
    }
}

3. 定义一个三角形类(Triangle),包含三个边长(a, b, c)属性,并实现一个判断是否为等边三角形的方法。

class Triangle {
    double a;
    double b;
    double c;

    public Triangle(double a, double b, double c) {
         = a;
         = b;
         = c;
    }

    public boolean isEquilateral() {
        return a == b && b == c;
    }
}

public class Main {
    public static void main(String[] args) {
        Triangle triangle = new Triangle(3, 3, 3);
        ("这个三角形是等边三角形吗?" + (() ? "是" : "不是"));
    }
}

4. 定义一个银行账户类(BankAccount),包含账户余额(balance)属性,实现存款(deposit)和取款(withdraw)方法。

class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
         = initialBalance;
    }

    public void deposit(double amount) {
        balance += amount;
        ("存款成功,当前余额:" + balance);
    }

    public void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            ("取款成功,当前余额:" + balance);
        } else {
            ("余额不足,取款失败");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount(1000);
        (500);
        (200);
    }
}

5. 定义一个动物类(Animal)和一个猫类(Cat),猫类继承动物类,并实现一个叫声方法。

class Animal {
    void makeSound() {
        ("动物叫声");
    }
}

class Cat extends Animal {
    @Override
    void makeSound() {
        ("喵喵喵");
    }
}

public class Main {
    public static void main(String[] args) {
        Cat cat = new Cat();
        ();

        // 对比 ();
        Animal animal = new Animal();
        ();
    }
}

6. 定义一个接口Flyable,包含一个飞行方法fly(),然后定义一个鸟类(Bird)实现该接口。

interface Flyable {
    void fly();
}

class Bird implements Flyable {
    @Override
    public void fly() {
        ("鸟儿在飞翔");
    }
}

public class Main {
    public static void main(String[] args) {
        Bird bird = new Bird();
        ();
    }
}

7. 定义一个抽象类Shape,包含一个计算面积的方法getArea(),然后定义一个圆形类(Circle)继承该抽象类,并实现计算面积的方法。

abstract class Shape {
    abstract double getArea();
}

class Circle extends Shape {
    double radius;

    public Circle(double radius) {
         = radius;
    }

    @Override
    double getArea() {
        return  * radius * radius;
    }
}

public class Main {
    public static void main(String[] args) {
        Circle circle = new Circle(5);
        ("圆形面积:" + ());
    }
}

8. 定义一个接口可食用(Edible),包含一个方法isEdible(),然后定义一个水果类(Fruit)实现该接口。

interface Edible {
    boolean isEdible();
}

class Fruit implements Edible {
    @Override
    public boolean isEdible() {
        return true;
    }
}

public class Main {
    public static void main(String[] args) {
        Fruit fruit = new Fruit();
        ("这个水果可以吃吗?" + (() ? "可以" : "不可以"));
    }
}

9. 定义一个接口可充电(Chargeable),包含一个方法charge(),然后定义一个手机类(SmartPhone)实现该接口。

interface Chargeable {
    void charge();
}

class SmartPhone implements Chargeable {
    @Override
    public void charge() {
        ("手机正在充电...");
    }
}

public class Main {
    public static void main(String[] args) {
        SmartPhone smartphone = new SmartPhone();
        ();
    }
}

10. 创建一个Shape工具类,并定义一个计算面积的方法,重载该方法以计算圆和矩形的面积。

class Shape {
    public static double PI = 3.1415926535;

    public static double area(double radius) {
        return  * radius * radius; // 圆面积
    }

    public static double area(double length, double width) {
        return length * width; // 矩形面积
    }
}

public class Main {
    public static void main(String[] args) {
        ("圆面积:" + (5.0));
        
        Shape shape = new Shape();
        ("矩形面积:" + (4.0, 6.0));
    }
}

11. 定义一个用户类(User),包含用户名(username)和密码(password)属性,提供静态方法验证密码是否有效(长度至少为6),并提供getter和setter方法。

class User {
    private String username;
    private String password;

    public User(String username, String password) {
         = username;
         = password;
    }

    public static boolean validatePassword(String password) {
        return () >= 6;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
         = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
         = password;
    }
}

public class Main {
    public static void main(String[] args) {
        User user = new User("Alice", "123456");
        ("用户名:" + ());
        ("密码是否有效:" + (()));
    }
}

12. 定义一个图书类(Book),包含书名(title)和作者(author)属性,提供一个静态方法来打印所有图书Book[]的信息,并提供getter和setter方法。

class Book {
    private String title;
    private String author;

    public Book(String title, String author) {
         = title;
         = author;
    }

    public static void printBooks(Book[] books) {
        for (Book book : books) {
            ("书名:" + () + ",作者:" + ());
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
         = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
         = author;
    }
}

public class Main {
    public static void main(String[] args) {
        Book[] books = {
            new Book("Java编程思想", "Bruce Eckel"),
            new Book("Effective Java", "Joshua Bloch")
        };
        (books);
    }
}

13. 定义一个学生类(Student),包含姓名(name)和成绩(score)属性,提供一个静态方法计算所有学生的平均成绩,并提供getter和setter方法。

class Student {
    private String name;
    private double score;

    public Student(String name, double score) {
         = name;
         = score;
    }

    public static double calculateAverageScore(Student[] students) {
        double totalScore = 0;
        for (Student student : students) {
            totalScore += ();
        }
        return totalScore / ;
    }

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
         = score;
    }
}

public class Main {
    public static void main(String[] args) {
        Student[] students = {
            new Student("张三", 90),
            new Student("李四", 85),
            new Student("王五", 92)
        };
        ("学生的平均成绩:" + (students));
    }
}

14. 定义一个学生类(Student),包含姓名(name)和成绩(score)属性。提供一个静态方法来找出最高分的学生,并提供getter和setter方法。

class Student {
    private String name;
    private double score;

    public Student(String name, double score) {
         = name;
         = score;
    }

    public static Student findTopStudent(Student[] students) {
        Student topStudent = students[0];
        for (Student student : students) {
            if (() > ()) {
                topStudent = student;
            }
        }
        return topStudent;
    }

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
         = score;
    }
}

public class Main {
    public static void main(String[] args) {
        Student[] students = {
            new Student("张三", 90),
            new Student("李四", 95),
            new Student("王五", 88)
        };
        Student topStudent = (students);
        ("最高分的学生是:" + () + ",分数为:" + ());
    }
}

15. 写一个简单的String工具类。提供几个基本的方法,检查字符串是否为空、首字母大写、反转字符串。

class StringUtil {

    public static boolean isEmpty(String str) {
        return str == null || ().isEmpty();
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String capitalize(String str) {
        if (isEmpty(str)) {
            return str;
        }
        char firstChar = ((0));
        if (() == 1) {
            return (firstChar);
        } else {
            return firstChar + (1);
        }
    }

    public static String reverse(String str) {
        if (str == null) {
            return null;
        }
        return new StringBuilder(str).reverse().toString();
    }

}

public class Main {
    public static void main(String[] args) {

        String str = "hello world";

        String capitalize = (str);
        (capitalize); //Hello world

        boolean notEmpty = (str);
        (notEmpty);  //true

        ((str)); //dlrow olleh
        
    }
}

16. 定义一个员工类Employee,包含姓名和年龄属性,以及一个显示信息的方法showInfo()。再定义两个子类Manager和Developer,分别重写showInfo()方法。在Main方法中,创建Manager和Developer对象,并通过向上转型为Employee类型,调用showInfo()方法。

class Employee {
    private String name;
    private int age;

    public Employee(String name, int age) {
         = name;
         = age;
    }

    public void showInfo() {
        ("员工姓名:" + name + ",年龄:" + age);
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

class Manager extends Employee {
    public Manager(String name, int age) {
        super(name, age);
    }

    @Override
    public void showInfo() {
        ("经理姓名:" + getName() + ",年龄:" + getAge());
    }
}

class Developer extends Employee {
    public Developer(String name, int age) {
        super(name, age);
    }

    @Override
    public void showInfo() {
        ("开发者姓名:" + getName() + ",年龄:" + getAge());
    }
}

public class Main {
    public static void main(String[] args) {
        Employee manager = new Manager("李四", 35);
        Employee developer = new Developer("王五", 28);

        ();
        ();
    }
}

17. 定义一个支付类Payment,包含一个抽象方法pay()。再定义两个子类CreditCardPayment和CashPayment,分别实现pay()方法。在Main方法中,创建CreditCardPayment和CashPayment对象,并通过向上转型为Payment类型,调用pay()方法。

abstract class Payment {
    abstract void pay(double amount);
}

class CreditCardPayment extends Payment {
    @Override
    void pay(double amount) {
        ("使用信用卡支付了:" + amount + "元");
    }
}

class CashPayment extends Payment {
    @Override
    void pay(double amount) {
        ("使用现金支付了:" + amount + "元");
    }
}

public class Main {
    public static void main(String[] args) {
        Payment creditCardPayment = new CreditCardPayment();
        Payment cashPayment = new CashPayment();

        (100);
        (50);
    }
}

18. 定义一个形状接口Shape,包含一个方法draw()。再定义两个实现类CircleShape和SquareShape,分别实现draw()方法。在Main方法中,创建CircleShape和SquareShape对象,并通过向上转型为Shape类型,调用draw()方法。

interface Shape {
    void draw();
}

class CircleShape implements Shape {
    @Override
    public void draw() {
        ("绘制圆形");
    }
}

class SquareShape implements Shape {
    @Override
    public void draw() {
        ("绘制正方形");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape circleShape = new CircleShape();
        Shape squareShape = new SquareShape();
        
        ();
        ();
    }
}

19. 定义一个父类Person和一个子类Student,都有introduce方法介绍自己,使用向上转型和向下转型调用introduce。

class Person {
    public void introduce() {
        ("我是一个人");
    }
}

class Student extends Person {
    public void introduce() {
        ("我是一个学生");
    }
}

public class Main {
    public static void main(String[] args) {
        Person person = new Student(); // 向上转型
        (); // 输出:我是一个学生

        Student student = (Student) person; // 向下转型
        (); // 输出:我是一个学生
    }
}

20. 定义一个Person基类和子类Administrator、Student和Teacher。每个子类都应重写一个show方法来展示各自类型的信息。Main方法中创建这些类的实例,并调用一个register方法来展示每个人的信息。register方法应接受一个Person类型的参数。

class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
         = name;
         = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public void show(){
        (name + ", " + age);
    }
}

class Administrator extends Person {
    @Override
    public void show() {
        ("管理员的信息为:" + getName() + ", " + getAge());
    }
}

class Student extends Person{
    @Override
    public void show() {
        ("学生的信息为:" + getName() + ", " + getAge());
    }
}

class Teacher extends Person{
    @Override
    public void show() {
        ("老师的信息为:" + getName() + ", " + getAge());
    }
}

public class Main {
    public static void main(String[] args) {
        //创建三个对象,并调用register方法

        Student s = new Student();
        ("张三");
        (18);
        
        Teacher t = new Teacher();
        ("王建国");
        (30);

        Administrator admin = new Administrator();
        ("管理员");
        (35);

        register(s);
        register(t);
        register(admin);

    }
    
    //这个方法既能接收老师,又能接收学生,还能接收管理员
    //只能把参数写成这三个类型的父类,向上转型是为了更好地调用子类方法
    public static void register(Person p){
        ();
    }
}