Java继承示例
下面是继承的示例Java继承示例,java,Java,下面是继承的示例 class Parent { Parent(int a, int b) { int c = a + b; System.out.println("Sum=" + c); } void display() { System.out.println("Return Statement"); } } class Child extends Parent { Child(int a, int b)
class Parent {
Parent(int a, int b) {
int c = a + b;
System.out.println("Sum=" + c);
}
void display() {
System.out.println("Return Statement");
}
}
class Child extends Parent {
Child(int a, int b) {
int c = a - b;
System.out.println("Difference=" + c);
}
}
public class InheritanceExample {
public static void main(String args[]) {
Child c = new Child(2, 1);
c.display();
}
}
当我没有非参数化的构造函数父级()时,我得到以下错误
线程“main”java.lang中出现异常。错误:未解决的编译问题:
隐式超级构造函数父级()未定义。必须显式调用另一个构造函数
在子级。(InheritanceExample.java:14)
在InheritanceExample.main(InheritanceExample.java:22)
你能解释一下基类中没有参数的构造函数的用途吗
class child extends parent
{
child(int a,int b)
{
int c=a-b;
System.out.println("Difference="+c);
}
}
子类构造函数必须做的第一件事是调用父类构造函数。
如果您没有明确地执行此操作(例如super(a,b)
),则会隐含对默认构造函数的调用(super()
)
要使其工作,您必须具有此默认构造函数(不带任何参数)
如果未声明任何构造函数,则会得到默认构造函数。如果至少声明一个构造函数,则不会自动获取默认构造函数,但可以再次添加它
您收到的错误消息是抱怨对super()
的隐含调用不起作用,因为父类中没有这样的构造函数
有两种解决方法:
super(a,b)
)另外,请不要使用所有小写的类名。我想在以下位置也有类似的问题:
您可以这样想:由于“child”继承自“parent”,因此“child”还必须是“parent”(多态性)的有效实例,才能作为“parent”本身。因此,“child”必须做的第一件事就是将自身构造为有效的“parent”——因此通过super()调用“parent”的构造函数必须是构造函数中的第一个方法调用。如果不存在这样的调用,则会隐式调用“parent”的无参数构造函数。它请求parent()的原因是child扩展了parent,但您没有在子构造函数中显式调用super(a,b)。由于没有显式调用父构造函数,javac尝试调用默认构造函数parent(),并在找不到它时发出抱怨 您可以通过以下代码看到这一点:
class parent
{
public parent() {
System.out.println("Parent Constructor");
}
public parent(int a,int b) {
int c=a+b;
System.out.println("Sum="+c);
}
public void display() {
System.out.println("Return Statement");
}
}
class child extends parent
{
public child(int a,int b) {
int c=a-b;
System.out.println("Difference="+c);
}
}
public class InheritanceExample
{
public static void main(String args[]) {
child c=new child(2,1);
c.display();
}
}
输出:
Parent Constructor
Difference=1
Return Statement
Sum=3
Difference=1
Return Statement
此外,这在没有默认构造函数的情况下也可以正常工作:
class parent
{
public parent(int a,int b) {
int c=a+b;
System.out.println("Sum="+c);
}
public void display() {
System.out.println("Return Statement");
}
}
class child extends parent
{
public child(int a,int b) {
super(a,b);
int c=a-b;
System.out.println("Difference="+c);
}
}
public class InheritanceExample
{
public static void main(String args[]) {
child c=new child(2,1);
c.display();
}
}
输出:
Parent Constructor
Difference=1
Return Statement
Sum=3
Difference=1
Return Statement
出现错误的原因是,如果我们不显式调用super,那么JVM会将super()放在子类的构造函数中,而这个super()会在父类中搜索一个没有参数的构造函数,而这个参数不在您的类中,因此它是错误的。要么在父类中放置一个非参数化构造函数,要么在子构造函数的第一行中放置语句super(a,b)
class Parent
{
Parent(int a, int b)
{
int c = a + b;
System.out.println("Sum=" + c);
}
void display()
{
System.out.println("Return Statement");
}
}
class Child extends Parent
{
Child(int a, int b)
{
super(a,b);
int c = a - b;
System.out.println("Difference=" + c);
}
}
class InheritanceExample
{
public static void main(String args[])
{
Child c = new Child(2, 1);
c.display();
}
}
在本例中,它只是创建父类实例的默认构造函数。通常您会将对象变量设置为默认值或null。请包含错误文本,而不是屏幕截图。这真的是源代码吗?它通过复制/粘贴为我编译并运行良好。@TofuBeer:如果在基类中包含不带参数的构造函数,它将编译。如果不是,则不是。在类中使用大写字母和在同一行中使用大括号是Java的惯例。
import java.io.*;
public class XXX
{
public static void main()throws IOException
{
System.out.println("Enter your name.");
String name = in.readLine();
System.out.println(name+" rules!! Thank You!!");
}
}