java 重写父类方法 构造函数
时间: 2023-11-03 18:03:06 浏览: 98
在Java中,重写父类方法是指子类继承自父类的方法,并对该方法进行了修改或者重新实现。重写父类方法的目的是为了更好地适应子类的需求,实现特定的功能。
在重写父类方法时,需要注意以下几点:
1. 方法名、参数列表及返回类型必须与父类方法相同。
2. 重写的方法不能比父类方法拥有更严格的访问权限。
3. 重写的方法不能抛出比父类方法更广泛的异常类型。
对于构造函数的重写,实际上并不是真正的重写,而是在子类中创建一个与父类构造函数相同的构造函数。当实例化子类对象时,会同时调用子类和父类的构造函数。子类的构造函数可以通过“super()”来调用父类的构造函数,并可以在其之前或之后添加特定的代码。
总结起来,重写父类的方法和构造函数可以使子类更适应特定的需求,实现自己的特定功能。
相关问题
重写父类的equals()方法
重写父类的equals()方法是在子类中重新定义equals()方法,以覆盖父类中的equals()方法。在重写equals()方法时,需要注意以下几点:
1. 方法签名必须与父类中的equals()方法相同,即相同的方法名、返回类型和参数列表。
2. 在重写equals()方法时,需要比较对象的某些属性是否相等,以确定两个对象是否相等。通常会比较对象的各个属性,例如学号、姓名、年龄等。
3. 重写equals()方法时,可以使用super关键字引用父类的equals()方法,以便在子类中调用父类的equals()方法。
4. 子类中重写equals()方法时,需要确保权限不小于父类的equals()方法。
以下是一个重写equals()方法的示例代码,假设Student类继承自Object类,并且两个Student对象的学号相同则判定为同一对象:
```java
class Student {
int id;
String name;
int age;
// 构造函数和其他方法省略
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Student student = (Student) obj;
return id == student.id;
}
}
```
编写Dog类继承Animal类,并在构造方法中调用父类构造方法,重写父类shout方法,方法体为打印输出:String.format("%s发出汪汪叫", this.name),添加public void superShout()方法,方法体为:调用父类shout方法
好的,下面是你要求的代码示例,演示如何编写一个Dog类继承Animal类,并在构造方法中调用父类构造方法,重写父类shout方法,添加public void superShout()方法,方法体为:调用父类shout方法:
```java
public class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public void shout() {
System.out.println(String.format("%s发出叫声", this.name));
}
}
public class Dog extends Animal {
private String breed;
public Dog(String name, String breed) {
super(name);
this.breed = breed;
}
@Override
public void shout() {
System.out.println(String.format("%s发出汪汪叫", this.name));
}
public void superShout() {
super.shout();
}
}
```
在这个示例中,Animal类包含一个字符串类型的name属性和一个带有一个字符串类型参数的构造函数,以及一个shout方法。Dog类继承Animal类,并添加了一个字符串类型的breed属性和一个带有两个字符串类型参数的构造函数。在Dog类中,我们重写了父类的shout方法,使它打印输出一个汪汪叫的字符串。我们还添加了一个public void superShout()方法,它调用父类的shout方法。这样,我们就可以创建一个Dog对象,并调用它的shout方法和superShout方法,它们分别会打印出不同的字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)