花几千上万学习Java,真没必要!(十七)

标准类制作:

 

测试代码1:创建一个Person类

package testclass.com;
//定义一个名为Person的类  
public class Person {  
 // 类的属性  
 // 使用private关键字限制外部直接访问,保证封装性  
 private String name; // 名字  
 private int age;     // 年龄  

 // 类的构造方法  
 // 构造方法名与类名相同,无返回类型  
 public Person() {  
     // 一个无参构造方法,用于创建Person对象时初始化其属性为默认值  
     // 对于基本数据类型,默认值为0、false、null等  
     // 对于对象类型,默认值为null  
 }  

 // 带参数的构造方法  
 public Person(String name, int age) {  
     this.name = name; // 使用this关键字区分成员变量和局部变量  
     this.age = age;  
 }  

 // 访问(Getter)方法,用于获取对象的属性值  
 public String getName() {  
     return name;  
 }  

 public int getAge() {  
     return age;  
 }  

 // 修改(Setter)方法,用于设置对象的属性值  
 public void setName(String name) {  
     this.name = name;  
 }  

 public void setAge(int age) {  
     // 添加一些验证逻辑,年龄不能小于0  
     if (age >= 0) {  
         this.age = age;  
     } else {  
         System.out.println("年龄不能小于0!");  
     }  
 }  

 // 展示Person信息的方法  
 public void displayInfo() {  
     System.out.println("姓名: " + name + ", 年龄: " + age);  
 }  

 // 主方法,程序的入口  
 public static void main(String[] args) {  
     // 使用无参构造方法创建Person对象  
     Person person1 = new Person();  
     person1.setName("张三");  
     person1.setAge(30);  
     person1.displayInfo(); // 展示person1的信息  

     // 使用带参数的构造方法创建Person对象  
     Person person2 = new Person("李四", 25);  
     person2.displayInfo(); // 展示person2的信息  
 }  
}

运行结果如下:

 

测试代码2:创建一个运动员类

package testclass.com;
public class FemaleAthlete {  
    // 类的属性  
    private String name; // 姓名  
    private String sport; // 运动项目  
    private int age; // 年龄  
    private String nationality; // 国籍  
    private double height; // 身高(米)  
    private double weight; // 体重(公斤)  
    private int medalCount; // 奖牌数  
    
  
    // 无参构造方法  
    public FemaleAthlete() {  
        this.name = "Unknown";  
        this.sport = "Unknown Sport";  
        this.age = 0;  
        this.nationality = "Unknown";  
        this.height = 0.0;  
        this.weight = 0.0;  
        this.medalCount = 0;  
    }  
  
    // 带多个参数的构造方法  
    public FemaleAthlete(String name, String sport, int age, String nationality, double height, double weight, int medalCount) {  
        this.name = name;  
        this.sport = sport;  
        this.age = age;  
        this.nationality = nationality;  
        this.height = height;  
        this.weight = weight;  
        this.medalCount = medalCount;  
    }  
  
      
   // getter和setter  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
  
    // 获取奖牌数的方法  
    public int getMedalCount() {  
        return medalCount;  
    }  
  
    // 设置奖牌数的方法(可选,根据需求添加)  
    public void setMedalCount(int medalCount) {  
        this.medalCount = medalCount;  
    }  
  
  
    // 显示运动员的详细信息  
    public void displayInfo() {  
        System.out.println("姓名: " + name);  
        System.out.println("运动项目: " + sport);  
        System.out.println("年龄: " + age);  
        System.out.println("国籍: " + nationality);  
        System.out.println("身高: " + height + "米");  
        System.out.println("体重: " + weight + "公斤");  
        System.out.println("奖牌数: " + medalCount);  
    }  
  
    // 添加奖牌  
    public void addMedal() {  
        medalCount++;  
    }  
  
    // 主函数,用于测试  
    public static void main(String[] args) {  
        // 使用带参构造方法创建对象  
        FemaleAthlete athlete = new FemaleAthlete("张丽丽", "游泳", 28, "中国", 1.72, 60, 3);  
          
        // 显示信息  
        athlete.displayInfo();  
          
        // 添加奖牌  
        athlete.addMedal();  
          
        // 显示添加奖牌后的信息  
        System.out.println("添加奖牌后:");  
        athlete.displayInfo(); // 或者直接输出奖牌数:System.out.println("添加奖牌后,奖牌总数为: " + athlete.getMedalCount());  
    }  
}

 

 运行结果如下:

测试代码3:创建一个汽车类

package testclass.com;
import java.util.ArrayList;  
import java.util.List;  
  
public class CarTest {  
    // 成员变量  
    private String brand;  
    private String model;  
    private int year;  
    private String engineType;  
    private int horsepower;  
    private List<String> colors;  
  
    // 构造方法  
    public CarTest(String brand, String model, int year, String engineType, int horsepower, List<String> colors) {  
        this.brand = brand;  
        this.model = model;  
        this.year = year;  
        this.engineType = engineType;  
        this.horsepower = horsepower;  
        this.colors = new ArrayList<>(colors); // 复制颜色列表以避免外部修改  
    }  
  
    // getter 和 setter 方法
    public String getBrand() {  
        return brand;  
    }  
  
    public void setBrand(String brand) {  
        this.brand = brand;  
    }  
  
    // 添加颜色的方法  
    public void addColor(String color) {  
        colors.add(color);  
    }  
  
    // 移除颜色的方法  
    public boolean removeColor(String color) {  
        return colors.remove(color);  
    }  
  
    // 检查车辆是否在指定年份之后生产的方法  
    public boolean isProducedAfter(int year) {  
        return this.year > year;  
    }  
  
    // 计算车辆价值的方法
    public double calculateValue() {  
        double baseValue = 20000;  
        double depreciation = (2023 - year) * 1000;  
        double engineBonus = horsepower * 50;  
        return baseValue - depreciation + engineBonus;  
    }  
  
    // 显示车辆信息的方法  
    public void displayInfo() {  
        System.out.println("Brand: " + brand);  
        System.out.println("Model: " + model);  
        System.out.println("Year: " + year);  
        System.out.println("Engine Type: " + engineType);  
        System.out.println("Horsepower: " + horsepower);  
        System.out.println("Colors: " + colors);  
        System.out.println("Estimated Value: $" + calculateValue());  
    }  
  
    // 主方法,用于测试TestCar类  
    public static void main(String[] args) {  
    	
        List<String> colors = List.of("Red", "Blue", "Black");  
  
        CarTest car = new CarTest("Tesla", "Model S", 2020, "Electric", 480, colors);  
        car.displayInfo();  
  
        car.addColor("White");  
        car.displayInfo(); // 现在将显示新增的颜色  
  
        boolean removed = car.removeColor("Blue");  
        if (removed) {  
            System.out.println("Color Blue has been removed.");  
        }  
  
        car.displayInfo(); // 再次显示信息,确认蓝色已被移除  
    }  
}

 

运行结果如下:

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值