0
import java.io.*;
import java.lang.*;
import java.util.*;
class First 
{
    public int No;
    public String Name; 
    void getDetails() throws IOException
    {
    Scanner sc = new Scanner(System.in);
    No = sc.nextInt();
    Name = sc.nextLine();
    }   
    void showDetails() throws IOException
    {
    System.out.println("The No and Name entered is: " +No  +"\t" +Name);
    }
}

class Second extends First
{
    public double Salary;   
    public void showSalary() throws IOException
    {
    BufferedReader br = new BufferedReader ( new InputStreamReader (System.in) );
    Salary = Double.parseDouble(br.readLine() );
    System.out.println(" The salary for no:" +No +"is" +Salary);
    }
}

class Demo_Inheritance 
{
    public static void main(String s[]) throws IOException
    {
     Second sd = new Second();
     sd.getDetails();
     sd.showDetails();
     sd.showSalary();
    First fs = new First();
    fs.getDetails();
    fs.showDetails();
    fs.showSalary(); }
}

当我执行此编码时,我在最后一行收到错误为“ fs.showSalary() ”-> 找不到符号“。(点)”

4

2 回答 2

2

该方法showSalary未在 中定义First。您需要添加方法或使用实例Second来使用该方法

于 2013-09-27T13:31:31.610 回答
2

编译器始终根据访问它的引用的声明类型来解析方法调用或字段访问。

由于声明的类型是fsis ,编译器会在类First中查找方法声明,但找不到。因此它给出了编译器错误。showSalary()First

于 2013-09-27T13:31:52.143 回答