设计Student类

Java基础入门课后习题第三单元第六题一


class Student{
	private String name;
	private double sore;
	public Student(){//无参构造方法
	}
	public Student(String name, double sore){//有参构造方法
		this.name = name;
		this.sore = sore;
	}
	public String getName(){
		return name;
	}
	public double getSore(){
		return sore;
	}
	public void setName(String name){
		this.name = name;
	}
	public void setSore(double sore){
		this.sore = sore;
	}
}
public class text {
	public static void main(String[] args) {
		Student stu1 =new Student();//第一种:用无参构造方法为name何sore赋值
		stu1.setName("李明");
		stu1.setSore(99.9);
		Student stu2 =new Student("张亮", 99.8);//第二种:用有参构造方法为name何sore赋值	
	}
}


### 定义实现 `Student` 在 Java 中,可以通过继承机制来扩展已有的功能。以下是基于提供的引用内容以及标准的面向对象编程原则设计的一个完整的 `Student` 。 #### 基本结构 假设有一个父 `Person`,它可能已经包含了基本的人的信息属性(如姓名、性别等)。通过让 `Student` 继承自 `Person`,可以减少重复代码并增强可维护性[^1]。 ```java // 父 Person class Person { private String name; private String sex; // 构造方法 public Person(String name, String sex) { this.name = name; this.sex = sex; } // Getter Setter 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Name: " + name + ", Sex: " + sex; } } ``` #### 子 `Student` 子 `Student` 可以增加额外的学生特定字段(如学号、班级领导状态),还可以提供一些新的行为或覆盖已有的行为[^2]。 ```java // 子 Student class Student extends Person { private int idNum; // 学生编号 private boolean isClassLeader; // 是否为班长 private double[] scores; // 成绩数组 // 构造方法 public Student(int idNum, String name, String sex, boolean isClassLeader, double[] scores) { super(name, sex); // 调用父构造器初始化基本信息 this.idNum = idNum; this.isClassLeader = isClassLeader; this.scores = scores; } // 新增的方法:study() public void study() { System.out.println("I am studying!"); } // 计算总成绩 sum() public double sum() { double total = 0; for (double score : scores) { total += score; } return total; } // 测试评分 testScore() public double testScore(double weightFactor) { double weightedSum = 0; for (int i = 0; i < scores.length; i++) { weightedSum += scores[i] * Math.pow(weightFactor, i); } return weightedSum / scores.length; } // 比较个学生的总成绩 compare() public int compare(Student other) { double myTotal = sum(); double otherTotal = other.sum(); if (myTotal > otherTotal) { return 1; } else if (myTotal < otherTotal) { return -1; } else { return 0; } } // Override equals() 方法 @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Student)) return false; Student that = (Student) obj; return idNum == that.idNum && Objects.equals(getName(), that.getName()); } // Override toString() 方法 @Override public String toString() { return super.toString() + ", ID Number: " + idNum + ", Is Class Leader: " + isClassLeader; } } ``` #### 使用示例 下面是一个简单的测试程序展示如何创建操作 `Student` 对象: ```java public class Main { public static void main(String[] args) { // 创建一个学生实例 Student s1 = new Student(1, "Alice", "Female", true, new double[]{85, 90, 78}); // 输出学生信息 System.out.println(s1); // 执行学习动作 s1.study(); // 获取总分 System.out.println("Total Score of Alice: " + s1.sum()); // 创建另一个学生实例 Student s2 = new Student(2, "Bob", "Male", false, new double[]{80, 88, 92}); // 比较人的分数 int result = s1.compare(s2); if (result > 0) { System.out.println("Alice has a higher total score than Bob."); } else if (result < 0) { System.out.println("Bob has a higher total score than Alice."); } else { System.out.println("Both have the same total score."); } } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值