super()用于调用父构造函数吗? 请解释super()。
当前回答
构造函数 在构造函数中,可以使用它而不带点来调用另一个构造函数。Super调用父类中的构造函数;调用该类中的构造函数:
public MyClass(int a) {
this(a, 5); // Here, I call another one of this class's constructors.
}
public MyClass(int a, int b) {
super(a, b); // Then, I call one of the superclass's constructors.
}
Super在父类需要初始化自身时很有用。这允许您只在一个构造函数中编写一次所有硬初始化代码,并从所有其他更容易编写的构造函数调用它,这很有用。
方法 在任何方法中,都可以使用它和一个点来调用另一个方法。Super.method()调用超类中的方法;this.method()调用该类中的一个方法:
public String toString() {
int hp = this.hitpoints(); // Calls the hitpoints method in this class
// for this object.
String name = super.name(); // Calls the name method in the superclass
// for this object.
return "[" + name + ": " + hp + " HP]";
}
super在某些情况下是有用的:如果你的类和你的超类有相同的方法,Java会假设你想在你的类中使用这个方法;Super允许你请求父类的方法。这只在提高代码可读性时有用。
其他回答
super()是用来调用父构造函数吗?
是的。
请解释一下超级()。
Super()是Super关键字的一种特殊用法,用于调用无参数的父构造函数。通常,super关键字可用于调用被重写的方法、访问隐藏字段或调用超类的构造函数。
下面是官方教程
调用无参数的超级构造函数只是浪费屏幕空间和程序员时间。编译器生成的代码完全相同,不管你写不写。
class Explicit() {
Explicit() {
super();
}
}
class Implicit {
Implicit() {
}
}
我愿意与代码分享我所了解的一切。
java中的super关键字是一个引用变量,用于引用父类对象。它主要用于以下情况:-
1. super与变量的使用:
class Vehicle
{
int maxSpeed = 120;
}
/* sub class Car extending vehicle */
class Car extends Vehicle
{
int maxSpeed = 180;
void display()
{
/* print maxSpeed of base class (vehicle) */
System.out.println("Maximum Speed: " + super.maxSpeed);
}
}
/* Driver program to test */
class Test
{
public static void main(String[] args)
{
Car small = new Car();
small.display();
}
}
输出:
Maximum Speed: 120
super with的使用方法:
/* Base class Person */
class Person
{
void message()
{
System.out.println("This is person class");
}
}
/* Subclass Student */
class Student extends Person
{
void message()
{
System.out.println("This is student class");
}
// Note that display() is only in Student class
void display()
{
// will invoke or call current class message() method
message();
// will invoke or call parent class message() method
super.message();
}
}
/* Driver program to test */
class Test
{
public static void main(String args[])
{
Student s = new Student();
// calling display() of Student
s.display();
}
}
输出:
This is student class
This is person class
3.super与构造函数的使用:
class Person
{
Person()
{
System.out.println("Person class Constructor");
}
}
/* subclass Student extending the Person class */
class Student extends Person
{
Student()
{
// invoke or call parent class constructor
super();
System.out.println("Student class Constructor");
}
}
/* Driver program to test*/
class Test
{
public static void main(String[] args)
{
Student s = new Student();
}
}
输出:
Person class Constructor
Student class Constructor
Super是一个关键词。在子类方法定义中使用它来调用父类中定义的方法。父类的私有方法不能被调用。只有public和protected方法可以被super关键字调用。类构造函数也使用它来调用父类的构造函数。
点击这里查看进一步的解释。
super关键字可用于调用超类构造函数和引用超类的成员
当您使用正确的参数调用super()时,实际上调用了构造函数Box,它通过使用相应参数的值来初始化变量width、height和depth。你只剩下初始化它的增值权重。如果有必要,现在可以将类变量Box设为私有。放在Box类私有修饰符的字段中,并确保您可以毫无问题地访问它们。
在超类中可以有多个重载版本的构造函数,因此可以使用不同的参数调用方法super()。程序将执行与指定参数匹配的构造函数。
public class Box {
int width;
int height;
int depth;
Box(int w, int h, int d) {
width = w;
height = h;
depth = d;
}
public static void main(String[] args){
HeavyBox heavy = new HeavyBox(12, 32, 23, 13);
}
}
class HeavyBox extends Box {
int weight;
HeavyBox(int w, int h, int d, int m) {
//call the superclass constructor
super(w, h, d);
weight = m;
}
}
推荐文章
- Intellij IDEA Java类在保存时不能自动编译
- 何时使用Mockito.verify()?
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 如何在Java中打印二叉树图?
- String.format()在Java中格式化双重格式
- com.jcraft.jsch.JSchException: UnknownHostKey
- Java中的操作符重载
- 如何加速gwt编译器?
- 在Hibernate中重新连接分离对象的正确方法是什么?
- 应该……接住环内还是环外?