How to change the class of son's function which inherited from father class?
时间: 2024-05-29 07:08:16 浏览: 99
To change the class of a son's function that is inherited from a father class, you can create a new class that inherits from the father class and override the son's function in the new class with the desired behavior. Alternatively, you can also directly modify the son's function in the son class to change its behavior, but this may not be recommended if you want to maintain the integrity of the original father class.
相关问题
Create a class called Employee whose objects are records for an employee. This class will be a derived class of the class Person which you will have to copy into a file of your own and compile. An employee record has an employee's name (inherited from the class Person), an annual salary represented as a single value of type double, a year the employee started work as a single value of type int and a national insurance number, which is a value of type String. Your class should have a reasonable number of constructors and accessor methods, as well as an equals method. Write another class containing a main method to fully test your class definition
Sure, I can help you with that. Here's an implementation of the Employee class:
```java
public class Employee extends Person {
private double annualSalary;
private int startYear;
private String nationalInsuranceNumber;
public Employee(String name, double annualSalary, int startYear, String nationalInsuranceNumber) {
super(name);
this.annualSalary = annualSalary;
this.startYear = startYear;
this.nationalInsuranceNumber = nationalInsuranceNumber;
}
public Employee(String name) {
super(name);
}
public double getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(double annualSalary) {
this.annualSalary = annualSalary;
}
public int getStartYear() {
return startYear;
}
public void setStartYear(int startYear) {
this.startYear = startYear;
}
public String getNationalInsuranceNumber() {
return nationalInsuranceNumber;
}
public void setNationalInsuranceNumber(String nationalInsuranceNumber) {
this.nationalInsuranceNumber = nationalInsuranceNumber;
}
public boolean equals(Employee other) {
return super.equals(other) &&
this.annualSalary == other.annualSalary &&
this.startYear == other.startYear &&
this.nationalInsuranceNumber.equals(other.nationalInsuranceNumber);
}
}
```
In this implementation, the Employee class extends the Person class, which contains the name field. The Employee class then adds three additional fields: annualSalary, startYear, and nationalInsuranceNumber.
The class contains two constructors - one that takes all four fields as arguments and another that only takes the name field. It also contains accessor and mutator methods for each of the fields.
Finally, there is an equals method that compares two Employee objects for equality based on all four fields.
Here's an example of how you might test this class:
```java
public class EmployeeTest {
public static void main(String[] args) {
Employee employee1 = new Employee("John Doe", 50000, 2010, "123-45-6789");
Employee employee2 = new Employee("Jane Doe", 50000, 2010, "123-45-6789");
System.out.println(employee1.equals(employee2)); // true
employee2.setStartYear(2011);
System.out.println(employee1.equals(employee2)); // false
}
}
```
In this example, we create two Employee objects with the same salary, start year, and national insurance number, but different names. We then test the equals method to verify that it correctly identifies these objects as equal based on all four fields.
We then modify the start year of employee2 and test the equals method again to verify that it correctly identifies these objects as unequal based on the start year field.
Create a HistoryPanel class that extends JPanel. The constructor of HistoryPanel takes as argument a model object of type Library, which you need to store in some private instance variable. Add to the HistoryPanel class two private methods called historyMax and historyMin that take an arraylist of integers as argument and return as result the maximum and minimum number in the arraylist, respectively (you can assume that the arraylist contains at least one number). Then add to the HistoryPanel class a private method called historyRange that takes an arraylist of integers as argument and returns as result the difference between the max and min of the integers in the arraylist, or returns as result 10 if the difference between the man and min of the integers in the arraylist is strictly less than 10. Override the protected void paintComponent(Graphics g) method inherited from JPanel, and, inside your new paintComponent method, draw graphically how the total number of books borrowed by all users of the library changes over time, as follows:
Here is the code for the HistoryPanel class:
```java
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
public class HistoryPanel extends JPanel {
private Library library;
public HistoryPanel(Library library) {
this.library = library;
}
private int historyMax(ArrayList<Integer> list) {
int max = Integer.MIN_VALUE;
for(int num : list) {
if(num > max) {
max = num;
}
}
return max;
}
private int historyMin(ArrayList<Integer> list) {
int min = Integer.MAX_VALUE;
for(int num : list) {
if(num < min) {
min = num;
}
}
return min;
}
private int historyRange(ArrayList<Integer> list) {
int difference = historyMax(list) - historyMin(list);
if(difference < 10) {
return 10;
}
return difference;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ArrayList<Integer> history = library.getHistory();
int numPoints = history.size();
int maxValue = historyMax(history);
int minValue = historyMin(history);
int range = historyRange(history);
int xSpacing = getWidth() / numPoints;
int ySpacing = getHeight() / range;
for(int i = 0; i < numPoints - 1; i++) {
int x1 = i * xSpacing;
int y1 = (history.get(i) - minValue) * ySpacing;
int x2 = (i + 1) * xSpacing;
int y2 = (history.get(i + 1) - minValue) * ySpacing;
g.drawLine(x1, getHeight() - y1, x2, getHeight() - y2);
}
}
}
```
In the constructor, we store the Library model object in a private instance variable. The three private methods `historyMax`, `historyMin`, and `historyRange` are used to calculate the maximum, minimum, and range of values in an arraylist of integers.
In the `paintComponent` method, we first retrieve the history data from the Library object and calculate the maximum, minimum, and range of values. We then calculate the spacing between points for both the x and y axes. Finally, we loop through each point in the history and draw a line between it and the next point.
Note that we use the `getHeight` method to adjust the y-coordinates of the points so that they are drawn from the bottom of the panel upwards. This is because the origin (0,0) of the coordinate system in Swing is at the top-left corner of the panel, whereas in most mathematical systems it is at the bottom-left corner.
阅读全文
相关推荐










