【发布时间】: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