簡體   English   中英

使用 public static void main(String args[]) 從子 class JAVA 獲取 output

[英]Using public static void main(String args[]) to get output from child class JAVA

我試圖了解這里的問題,它表示命令執行失敗,但我似乎無法在這里找到問題,但我確實知道其中一個原因是 public static void main(String args{}) is not functioning as希望如此。

我的代碼在下面:

import java.util.*;

public class Children {
    public static void main(String args[]) {
       public String name;
       public int friends;

       public Children(String name) {
           this.name = name;
           friends = 0;
       }
       public void getFriend(Children k) {
           friends++;
       }

       public int numFriends() {
           return friends;
       }

       public String playsalong() {
           return "joy";
       }

       public String toString() {
           return "a child named "+name;
       }
   }
}

class Girl extends Children {
   public static void main(String args[]) {
       public Girl(String name) {
           super(name);
        friends = 1;
       }

       public void getFriend(Children k) {
           if (k instanceof Girl)
           friends += k.numFriends( );
           else
           friends++;
       }

       public String toString( ) {
           return "a girl named "+ name;
       } 
   }
}

class Boy extends Children {
   public static void main(String args[]) {
       public Boy(String name) {
           super(name);
       }

       public String toString() {
           return "a boy named " +name; 
       } 
   }
}

訪問修飾符應用於實例變量,它不能應用於局部 scope 內部,無論 static 字段是或否,它用於描述其他 object 的訪問級別,object 的類查看實例成員。

另一個問題是你試圖在主要方法內部和 class Girls 本身之外定義 class Girl 的構造函數,這是不允許的 class constructor 應該在 class 內部,例如 scope 不在任何其他方法中 882166088195188 或 882166088167837

構造函數應該在它的 class 的實例 scope 中定義,而不是在其他 class 中定義

所以

public Girl(String name) {
       super(name);
    friends = 1;
   }

此代碼應在女孩 class 中定義,並確保在實例 scope 中定義,而不是 static 或本地 scope。

對於其他方法,您只需要一個花括號“}”來結束主要方法,其他方法將被單獨定義。

對於 class 兒童同樣適用

class Boy 的構造函數應替換為 Children 構造函數並在 main 方法外定義。

方法 toString 真的很好只需要花括號 } 就可以在它包含 main 方法之前,因為在這里你在 main 方法中定義它。

這是有效的孩子 class:

public class Children {
   public String name;
   public int friends;
   
   public Children(String name) {
        super()// this optional because parent class define only no- 
                     // argument constructor
       this.name = name;
       friends = 0;
   }

public static void main(String args[]) {

       Children children = new Children();
             children.getFriend();
             children.numFriends();
             children.playsalong();
             children.toString() ;
  
  }

   public void getFriend(Children k) {
       friends++;
   }

   public int numFriends() {
       return friends;
   }

   public String playsalong() {
       return "joy";
   }

   public String toString() {
       return "a child named "+name;
   }
 

}

這里是女孩 class 的定義

class Girl extends Children {
      public Children(String name) {
   // super(name); /*here superclass do not define any constructor 
                  with String argument so this will not compile*/
       super()// this is what required and it optional as said before

       friends = 1;
   }
 public static void main(String args[]) {
            
            Girls girl = new Girl();
   
      }

   public void getFriend(Children k) {
       if (k instanceof Girl)
       friends += k.numFriends( );
       else
       friends++;
   }

   public String toString( ) {
       return "a girl named "+ name;
   } 

}

男孩 class 定義

class Boy extends Children {
  public Boy(String name) {
     super(name);/* this is fine and it mendatory since parent class 
          Children has a constructor with String parameter, it should 
          be called like this or code will not compile */
    }
   public static void main(String args[]) {

      Boy boy = new Boy("Alex");
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM