简体   繁体   English

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

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

I am trying to understand the issue here, it stated the command execution failed, but I cannot seem to find the issue here but I do get it that one of the reasons is public static void main(String args{}) is not functioning as I hope so.我试图了解这里的问题,它表示命令执行失败,但我似乎无法在这里找到问题,但我确实知道其中一个原因是 public static void main(String args{}) is not functioning as希望如此。

My code is down below:我的代码在下面:

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; 
       } 
   }
}

the access modifiers are applied in instance variable, it can not be applied inside local scope what ever is static field or not, it's for describing the access level for other object, object of classes look at instance membres.访问修饰符应用于实例变量,它不能应用于局部 scope 内部,无论 static 字段是或否,它用于描述其他 object 的访问级别,object 的类查看实例成员。

another issue you are trying to define a constructor of class Girl inside the main method and outside the class Girls itself, which is not allowed class consructor should be inside that class and in instance scope not in any other method static or not static.另一个问题是你试图在主要方法内部和 class Girls 本身之外定义 class Girl 的构造函数,这是不允许的 class constructor 应该在 class 内部,例如 scope 不在任何其他方法中 882166088195188 或 882166088167837

constructor should be defined in an instance scope of it's class not in other class构造函数应该在它的 class 的实例 scope 中定义,而不是在其他 class 中定义

so所以

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

this code should be defined in Girl class and be sure that is defined inside instance scope not static or local scope.此代码应在女孩 class 中定义,并确保在实例 scope 中定义,而不是 static 或本地 scope。

for other methods you just need an curly brace "}" to end the main method the other methods will be defined speratly.对于其他方法,您只需要一个花括号“}”来结束主要方法,其他方法将被单独定义。

for the class Children same thing appilied对于 class 儿童同样适用

constructor of class Boy should be replaced by Children constructor and defined outside main method. class Boy 的构造函数应替换为 Children 构造函数并在 main 方法外定义。

method toString is really fine just need curly brace } befor it to encolse the main method, because here you are defining it inside main method.方法 toString 真的很好只需要花括号 } 就可以在它包含 main 方法之前,因为在这里你在 main 方法中定义它。

here is the valid Children class:这是有效的孩子 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;
   }
 

} }

and here the Girl class definition这里是女孩 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;
   } 

} }

the boy class definition男孩 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.

相关问题 如何在不使用 public static void main (String[]args) 的情况下在 vs 代码中获取 Java 程序中的用户输入 - How to get user input in java programs in vs code without using public static void main (String[]args) 如何在 Java 中调用公共 static void main(String[] args) class 中的方法? - How to call a method inside the public static void main(String[] args) class in Java? 使用 int 而不是 String:public static void main (int[] args) - Using int Instead Of String: public static void main (int[] args) 了解公共静态void main(String [] args) - Understanding public static void main(String[] args) 在ActivityTime类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Main method not found in class ActivityTime, please define the main method as: public static void main(String[] args) 错误:在类mainGUI中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class mainGUI, please define the main method as: public static void main(String[] args) “错误:在Grad类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args)” - “Error: Main method not found in class Grad, please define the main method as: public static void main(String[] args)” 错误:在 class 中找不到主要方法,请将主要方法定义为:public static void main(String[] args) - Error: Main method not found in class, please define the main method as: public static void main(String[] args) 错误:在类Text中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Text, please define the main method as: public static void main(String[] args) 错误:在Binary类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Binary, please define the main method as: public static void main(String[] args)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM