java—无法通过子类访问父类方法

lb3vh1jj  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(432)

这是我的密码:

static class Developer
{
String Name;
String Language;
int Years;
int Age;

void Company ()
{
  System.out.println (Name + " Codes in " + Language + " & has " + Years +
          " Years of experience" + ", He is " + Age + " Old");
}

public Developer ()
{
  System.out.println ("Hello");
}

public Developer (String Name, String Language, int Years, int Age)
{
  this ();
  this.Name = Name;
  this.Language = Language;
  this.Years = Years;
  this.Age = Age;
}

class Analyst extends Developer
{
  public Analyst (String Name, String Language, int Years, int Age)
  {
super (Name, Language, Years, Age);
  }
}
} 

public static void main (String[]args)
{

Developer d1 = new Developer ();
d1.Name = "John";
d1.Language = "Java";
d1.Years = 2;
d1.Age = 22;

Developer d2 = new Developer ("Doe", "C++", 3, 23);

d1.Company ();
d2.Company ();

Analyst A1 = new Analyst ("Doe", "C++", 3, 23);
A1.Company ();
  }
}

获取以下错误:

Main.java:58: error: cannot find symbol
   Analyst A1 = new Analyst("Doe","C++",3,23);
   ^
    symbol:   class Analyst
   location: class Main
   Main.java:58: error: cannot find symbol
   Analyst A1 = new Analyst("Doe","C++",3,23);
                 ^
  symbol:   class Analyst
  location: class Main
  2 errors
txu3uszq

txu3uszq1#

public class Developer
{
String Name;
String Language;
int Years;
int Age;

void Company ()
{
  System.out.println (Name + " Codes in " + Language + " & has " + Years +
          " Years of experience" + ", He is " + Age + " Old");
}

public Developer ()
{
  System.out.println ("Hello");
}

public Developer (String Name, String Language, int Years, int Age)
{
  this ();
  this.Name = Name;
  this.Language = Language;
  this.Years = Years;
  this.Age = Age;
}
 }

 public class Analyst extends Developer {

    public Analyst (String Name, String Language, int Years, int Age){
        super (Name, Language, Years, Age);
      }

    public static void main (String[]args){

    Developer d1 = new Developer ();
    d1.Name = "John";
    d1.Language = "Java";
    d1.Years = 2;
    d1.Age = 22;

    Developer d2 = new Developer ("Doe", "C++", 3, 23);

    d1.Company ();
    d2.Company ();

    Analyst A1 = new Analyst ("Doe", "C++", 3, 23);
    A1.Company ();
      }

    }
fzsnzjdm

fzsnzjdm2#

您不能使用 static 关键字,除非它是内部类。一 static 内部类是一个嵌套类,它是 static 外层阶级的成员。

public class Outer {
   static class InnerStaticClass {
      public void example() {
         System.out.println("This is static nested class");
      }
   }
   public static void main(String args[]) {
      Outer.InnerStaticClass nestedClass = new Outer.InnerStaticClass();
      nestedClass.example();
   }
}

相关问题