已編輯:現在看起來更干凈了,反映了我目前所處的位置以及我正在努力完成的作業,并顯示了我正在處理的新問題(它下面有一條評論,解釋了我正在得到什么)。
public class Main {
class Terrain
{
private int length, width;
Terrain(int l, int w)
{
length = l;
width = w;
}
public String getTerrainSize()
{
return "Land has dimensions " length " X " width;
}
}
public class Mountain extends Terrain{
private int mounts;
public Mountain(int num, int x, int y) {
//error above: Implicit super constructor Main.Terrain() is undefined. Must explicitly invoke another constructor
//What exactly does this error mean, and how should I fix it?
mounts = num;
length = x;
width = y;
}
public String getMountainSize()
{
return "Mountains have the dimensions " length " X " width " with a total of " mounts " mountains";
}
}
public static void main(String[] args) {
Terrain T1 = new Terrain(400, 200);
T1.getTerrainSize();
Mountain M1 = new Mountain(350, 150);
//error here: The constructor Main.Mountain(int, int) is undefined
//I have a feeling it'll be fixed if I fix the first error, but if not, let me know.
M1.getMountainSize();
}
}
對不起,如果帖子有點長,但我希望每個人都能看到整個畫面。
uj5u.com熱心網友回復:
最后:
public class Main {
static class Terrain
{
public static int length, width;
Terrain(int l, int w)
{
length = l;
width = w;
}
public String getTerrainSize()
{
return "Land has dimensions " length " X " width;
}
}
public static class Mountain extends Terrain{
private int mounts;
public Mountain(int num, int x, int y) {
super(length, width);
mounts = num;
length = x;
width = y;
}
public String getMountainSize()
{
return "Mountains have the dimensions " length " X " width " with a total of " mounts " mountains";
}
}
public static void main(String[] args) {
Terrain T1 = new Terrain(400, 200);
System.out.println(T1.getTerrainSize());
Mountain M1 = new Mountain(8, 350, 150);
System.out.println(M1.getMountainSize());
}
}
uj5u.com熱心網友回復:
為了澄清,您需要知道位元組碼級別才能知道事情是如何作業的。
讓我們開始使用這段代碼:
public class ParentConstructor {
public ParentConstructor() {
}
}
然后運行編譯(javac-java compile)并提取資訊(javap-java print)命令:
$~ javac /path/to/ParentConstructor.java
$~ javap /path/to/ParentConstructor.class
你會得到:
{
public ParentConstructor();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 2: 0
line 3: 4
}
第一條指令壓this入運算元堆疊。第二條指令從堆疊中彈出這個值并呼叫類中<init>定義的方法Object。這對應于super()呼叫,即對超類的建構式的呼叫Object。
ParentConstructor可以改寫如下:
public class ParentConstructor {
public ParentConstructor() {
super();
}
}
這意味著如果超類具有默認建構式,則可以在子類建構式中隱式呼叫超類的建構式,如果超類沒有默認建構式,則可以在子類建構式中顯式呼叫超類的建構式。
public static class Terrain
{
public int length, width;
Terrain(int l, int w)
{
length = l;
width = w;
}
public String getTerrainSize()
{
return "Land has dimensions " length " X " width;
}
}
public static class Mountain extends Terrain{
private int mounts;
public Mountain(int num, int x, int y) {
//explicitly call superclass's constructor.
super(x, y);
//Error above, I'm not sure why this wont work when it works just fine in the parent class.
mounts = num;
length = x;
width = y;
}
public String getMountainSize()
{
return "Mountains have the dimensions " length " X " width " with a total of " mounts " mountains";
}
}
或向超類添加默認建構式。
public static class Terrain
{
public int length, width;
Terrain(int l, int w)
{
length = l;
width = w;
}
public Terrain() {
}
public String getTerrainSize()
{
return "Land has dimensions " length " X " width;
}
}
參考:
- 方法 java/lang/Object."<init>":()V 的定義在哪里?
- 建構式位元組碼
uj5u.com熱心網友回復:
嘗試從 w3School 學習 Java 建構式。
如果您在方法中訪問長度或寬度值。您必須 將此關鍵字放在長度或寬度之前。
代碼 :
public class Terrain
{
private int length, width;
public Terrain(int l, int w)
{
length = l;
width = w;
}
public String getTerrainSize()
{
return "Land has dimensions " this.length " X " this.width;
}
}
public class Mountain extends Terrain{
private int mounts,length,width;
public Mountain(int num, int x, int y) {
mounts = num;
length = x;
width = y;
}
public String getMountainSize()
{
return "Mountains have the dimensions " this.length " X " this.width " with a total of " this.mounts " mountains";
}
}```
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459968.html
上一篇:魔術吸氣劑繼承
