【问题标题】:Having problem with method calling in Inheritance继承中的方法调用有问题
【发布时间】:2020-01-15 16:27:33
【问题描述】:

我在我的程序中使用了实现继承,而我的子类中的方法没有在主方法中被调用。它显示错误“方法 getArea() 未在类型 Second 中定义”。 getPerimeter() 方法也有同样的问题。

我已经尝试设置值和更改参数。

package firstproject;
import java.util.Scanner;
import java.util.Date;
import java.util.ArrayList;
public class Second{
  public String color="red" ;
  public boolean filled;
public Second() {

}
public Second(String tcolor, boolean tfilled) {
  tcolor=color;
  tfilled=filled;
}
public String getColor() {
  return color;
}
public boolean getfilled() {
   return filled;
}
public void setColor(String tcolor) {
   tcolor=color; 
}
public void setFilled(boolean tfilled) {
   tfilled=filled;
}
public String toString() {
   return "Color is =" +color+ " and it is filled or not = " 
   +filled;
 }
class myclass extends Second {
   double s1=1.0;
   double s2=1.0;
   double s3=1.0;
public myclass(){

 }
public myclass(double s4, double s5, double s6) {
s4=s1;
s5=s2;
s6=s3;
 }
double gets1() {
return s1;
}

double gets2() {
return s2;
}
double gets3() {
return s3;
}
public void sets1(double s4) {
s4=s1;
}
public void sets2(double s5) {
s5=s2;
}
public void sets3(double s6) {
s6=s3;
}
public double getArea() {
return (s2*s3)/2;
}
public double getPerimeter() {
return s1+s2+s3;
}
}
public static void main(String[] args) {
    System.out.println("Enter the three sides = ");
    Scanner input=new Scanner(System.in);
    int side1=input.nextInt();
    int side2=input.nextInt();
    int side3=input.nextInt();
    Second Triangle= new Second();
    System.out.println("Enter the color = ");
    String colo=input.next();
    System.out.println("The boolean value = ");
    String fil =input.next();
    System.out.println("The area of the triangle is = " 
  +Triangle.getArea());
    System.out.println("The perimeter of the triangle is = " 
  +Triangle.getPerimeter());
    System.out.println("The color in which it is filled is = " 
  +Triangle.getColor());
    System.out.println("If it is filled or not = " 
  +Triangle.getfilled());
}
}

It's showing the error "The method getArea() is not defined in type 
Second". Also, the same stuff is happening with the getPerimeter() 
method. So, I had the question of how to solve the code and is it an   
error related to subclass? The question is something like: 
(The Triangle class) Design a class named Triangle that extends
GeometricObject. The class contains:
■ Three double data fields named side1, side2, and side3 with 
default values
1.0 to denote three sides of the triangle.
■ A no-arg constructor that creates a default triangle.
■ A constructor that creates a triangle with the specified side1, 
side2, and
side3.
■ The accessor methods for all three data fields.
■ A method named getArea() that returns the area of this triangle.
■ A method named getPerimeter() that returns the perimeter of this 
triangle.
■ A method named toString() that returns a string description for 

三角形。 有关计算三角形面积的公式,请参见编程练习 2.15。 toString() 方法实现如下: return "三角形:side1 = " + side1 + " side2 = " + side2 + " 边 3 = " + 边 3; 为 Triangle 和 GeometricObject 类绘制 UML 图 并实现类。编写一个提示用户输入的测试程序 三角形的三个边、一个颜色和一个布尔值,用于指示是否 三角形被填满。程序应该用这些创建一个三角形对象 边并使用输入设置颜色和填充属性。该程序 应显示面积、周长、颜色,并以真假表示是否 是否填满。

【问题讨论】:

    标签: java


    【解决方案1】:

    认为您对多态性继承感到困惑。

    Java 中的

    继承是一种机制,其中一个对象获取父对象的所有属性和行为。它是OOPs(面向对象编程系统)的重要组成部分。

    Java 中继承背后的理念是,您可以创建基于现有类的新类。从现有类继承时,可以重用父类的方法和字段。此外,您还可以在当前类中添加新方法和字段。

    继承表示 IS-A 关系,也称为父子关系。

    Java 中的

    多态性 是一个概念,通过它我们可以以不同的方式执行单个操作。多态性源自 2 个希腊词:poly 和 morphs。 “poly”一词表示许多,“morphs”表示形式。所以多态意味着多种形式。

    Java 中有两种类型的多态:编译时多态和运行时多态。我们可以通过方法重载和方法覆盖在java中实现多态。

    如果在 Java 中重载静态方法,就是编译时多态的例子。在这里,我们将重点介绍 java 中的运行时多态性。

    你在这里所做的是继承。所以父类方法的属性和方法被继承给子类,反之亦然。在您的情况下,Second 是父类,myClassSecond 类的子类。由于getArea 方法定义在myClass 中,它是一个子类,所以父类Second 没有关于getArea 方法的详细信息。所以你得到了这个错误。

    【讨论】:

    • 是的,谢谢!那么我应该怎么做才能在父类中添加更多细节呢?
    • 技术上这是不可能的。但是你想通过这样做来实现什么?
    • 我只是想知道错误到底是什么以及解决方案,以免再次发生
    • 你已经创建了 Second 类的对象为 Second Triangle= new Second();访问方法,而是应该将 myclass 的对象创建为 myclass triangle = new myClass();使用该方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2014-06-18
    • 2014-10-25
    • 2021-06-17
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多