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

 💥 该系列属于【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) {
        this.name = name;
        this.age = age;
    }

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

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

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

class Rectangle {
    double length;
    double width;

    public Rectangle(double length, double width) {
        this.length = length;
        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(5, 3);
        System.out.println("矩形面积:" + rectangle.calculateArea());
        System.out.println("矩形周长:" + rectangle.calculatePerimeter());
    }
}

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

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

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = 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);
        System.out.println("这个三角形是等边三角形吗?" + (triangle.isEquilateral() ? "是" : "不是"));
    }
}

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

class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
        this.balance = initialBalance;
    }

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

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

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

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

class Animal {
    void makeSound() {
        System.out.println("动物叫声");
    }
}

class Cat extends Animal {
    @Override
    void makeSound() {
        System.out.println("喵喵喵");
    }
}

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

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

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

interface Flyable {
    void fly();
}

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

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

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

abstract class Shape {
    abstract double getArea();
}

class Circle extends Shape {
    double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

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

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

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();
        System.out.println("这个水果可以吃吗?" + (fruit.isEdible() ? "可以" : "不可以"));
    }
}

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

interface Chargeable {
    void charge();
}

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

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

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

class Shape {
    public static double PI = 3.1415926535;

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

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

public class Main {
    public static void main(String[] args) {
        System.out.println("圆面积:" + Shape.area(5.0));
        
        Shape shape = new Shape();
        System.out.println("矩形面积:" + shape.area(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) {
        this.username = username;
        this.password = password;
    }

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

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

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

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

class Book {
    private String title;
    private String author;

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

    public static void printBooks(Book[] books) {
        for (Book book : books) {
            System.out.println("书名:" + book.getTitle() + ",作者:" + book.getAuthor());
        }
    }

    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 class Main {
    public static void main(String[] args) {
        Book[] books = {
            new Book("Java编程思想", "Bruce Eckel"),
            new Book("Effective Java", "Joshua Bloch")
        };
        Book.printBooks(books);
    }
}

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

class Student {
    private String name;
    private double score;

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

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

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

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

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

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

class Student {
    private String name;
    private double score;

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

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

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

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

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

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

class StringUtil {

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

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

    public static String capitalize(String str) {
        if (isEmpty(str)) {
            return str;
        }
        char firstChar = Character.toUpperCase(str.charAt(0));
        if (str.length() == 1) {
            return String.valueOf(firstChar);
        } else {
            return firstChar + str.substring(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 = StringUtil.capitalize(str);
        System.out.println(capitalize); //Hello world

        boolean notEmpty = StringUtil.isNotEmpty(str);
        System.out.println(notEmpty);  //true

        System.out.println(StringUtil.reverse(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) {
        this.name = name;
        this.age = age;
    }

    public void showInfo() {
        System.out.println("员工姓名:" + 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() {
        System.out.println("经理姓名:" + getName() + ",年龄:" + getAge());
    }
}

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

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

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

        manager.showInfo();
        developer.showInfo();
    }
}

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) {
        System.out.println("使用信用卡支付了:" + amount + "元");
    }
}

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

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

        creditCardPayment.pay(100);
        cashPayment.pay(50);
    }
}

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

interface Shape {
    void draw();
}

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

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

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

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

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

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

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

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

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) {
        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 show(){
        System.out.println(name + ", " + age);
    }
}

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

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

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

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

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

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值