这个Java多态性分配准备好交上来了吗? 问题:开发“Shape”应用程序,以便:
以任意顺序实现各种类型(所有五个类)的对象数组。在某种类型的循环结构中,通过调用draw、move和erase这三种方法来演示多态性。也就是说,在花括号中,只有三个方法调用。验证输出消息是否来自所有三个方法以及所有七个类。这个Java多态性分配准备好交上来了吗? 问题:开发“Shape”应用程序,以便:,java,arrays,polymorphism,Java,Arrays,Polymorphism,以任意顺序实现各种类型(所有五个类)的对象数组。在某种类型的循环结构中,通过调用draw、move和erase这三种方法来演示多态性。也就是说,在花括号中,只有三个方法调用。验证输出消息是否来自所有三个方法以及所有七个类。 您应该为此类开发的唯一类是测试应用程序。上周的七节课应该保持不变 考试课有必要吗?我无法通过这种方式获得任何输出。 在运行开始时,我还得到了2个默认构造函数。 我应该为其中任何一个使用超级类吗?任何其他提示都将不胜感激 public class Shapes{ p
您应该为此类开发的唯一类是测试应用程序。上周的七节课应该保持不变 考试课有必要吗?我无法通过这种方式获得任何输出。 在运行开始时,我还得到了2个默认构造函数。 我应该为其中任何一个使用超级类吗?任何其他提示都将不胜感激
public class Shapes{
public static void main(String[] args) {
Shapes [ ] Array = {
new Triangle ( " ", " ", " "),
new Rectangle ( " ", " ", " "),
new Square ( " ", " ", " "),
new Ellipse ( " ", " ", " "),
new Circle ( " ", " ", " ")};
//public void TestShapes (Shapes[] Array ) {
for(int x =0; x<Array.length;x++)//iterates through array
{
System.out.println(x);
System.out.println(Array[x].toString());
}
}
}
public class Triangle extends Shapes {
private String draw;
private String erase;
private String move;
public String draw () {return draw; }
public void draw (String d) {d = d ; }
public String erase () {return erase; }
public void erase (String e) { e = e ; }
public String move () { return move; }
public void move (String m) { m = m; }
public Triangle ( ) {
draw = " ";
erase = " ";
move = " ";
System.out.println( "Default constructor " );
}
public Triangle (String d, String e, String m ) {
draw = " Triangle - draw method \n";
erase = " Triangle - erase method \n";
move = " Triangle - move method \n";
// System.out.println( draw + erase + move);
}
public String toString()
{
String s = "";
s+= /*" Triangle draw " + */ this.draw;
s+= /*" Triangle erase " + */ this.erase;
s+= /*" Triangle move " + */ this.move;
return s;
}
}
public class Rectangle extends Shapes {
private String draw;
private String erase;
private String move;
public String draw () {return draw; }
public void draw (String d) {d = d ; }
public String erase () {return erase; }
public void erase (String e) { e = e ; }
public String move () { return move; }
public void move (String m) { m = m; }
public Rectangle ( ) {
draw = " ";
erase = " ";
move = " ";
System.out.println( " Default constructor" );
}
public Rectangle (String d, String e, String m ) {
draw = " Rectangle - draw method\n";
erase = " Rectangle - erase method\n";
move = " Rectangle - move method\n";
//System.out.println( draw + erase + move );
}
public String toString()
{
String s = "";
s+= this.draw;
s+= this.erase;
s+= this.move;
return s;
}
}
public class Square extends Rectangle {
private String draw;
private String erase;
private String move;
public String draw () {return draw; }
public void draw (String d) {d = d ; }
public String erase () {return erase; }
public void erase (String e) { e = e ; }
public String move () { return move; }
public void move (String m) { m = m; }
public Square ( ) {
draw = " ";
erase = " ";
move = " ";
System.out.println( " Default constructor" );
}
public Square (String d, String e, String m ) {
draw = " Square - draw method \n";
erase = " Square - erase method \n";
move = " Square - move method\n";
// System.out.println( draw + erase + move );
}
public String toString()
{
String s = "";
s+= /*" square draw " +*/ this.draw;
s+= /*" square erase " + */ this.erase;
s+= /*" square move " + */ this.move;
return s;
}
}
public class Ellipse extends Shapes {
private String draw;
private String erase;
private String move;
public String draw () {return draw; }
public void draw (String d) {d = d ; }
public String erase () {return erase; }
public void erase (String e) { e = e ; }
public String move () { return move; }
public void move (String m) { m = m; }
public Ellipse ( ) {
draw = " ";
erase = " ";
move = " ";
System.out.println( " Default constructor" );
}
public Ellipse (String d, String e, String m ) {
draw = " ellipse - draw method \n ";
erase = "ellipse - erase method \n";
move = " ellipse - move method \n";
// System.out.println( draw + erase + move);
}
public String toString()
{
String s = "";
s+= /*" ellipse draw " +*/ this.draw;
s+= /*" ellipse erase " +*/ this.erase;
s+= /*" ellipse " + */ this.move;
return s;
}
}
public class Circle extends Ellipse {
private String draw;
private String erase;
private String move;
public String draw () {return draw; }
public void draw (String d) {d = d ; }
public String erase () {return erase; }
public void erase (String e) { e = e ; }
public String move () { return move; }
public void move (String m) { m = m; }
public Circle ( ) {
draw = " ";
erase = " ";
move = " ";
System.out.println( "Default constructor " );
}
public Circle (String d, String e, String m) {
draw = "Circle - draw method \n";
erase = "Circle - erase method \n";
move = "Circle - move method \n";
//System.out.println( draw + erase + move );
}
public String toString()
{
String s = "";
s+= /*" circle draw " + */ this.draw;
s+= /* " circle erase " + */ this.erase;
s+= /* " circle move " + */ this.move;
return s;
}
}
公共类形状{
公共静态void main(字符串[]args){
形状[]数组={
新三角形(“,”,“),
新矩形(“,”,“),
新广场(“,”,“),
新椭圆(“,”,“),
新圆(“,”,“)};
//公共void TestShapes(Shapes[]数组){
对于(intx=0;x我不会给那篇文章打很高的分数
对于初学者来说,您似乎不了解继承的好处。通过扩展Shapes
类,您实际上没有获得任何好处。该类应该充当应用程序的测试程序。是否应该有接口
或抽象
基类Shape
这将给您带来很多意义,因为Shape
可能看起来如下所示:
public abstract class Shape {
private String draw;
private String erase;
private String move;
// Put all other common stuff in here.
// For methods that should be implemented by
// each sub-shape mark it abstract and provide
// the specific implementation there.
}
如果有一个像上面这样的公共基类,为什么要像使用Rectangle
和Square
那样重复其中的所有类成员呢?这是完全不必要的,而且会破坏继承的好处;子类继承超级类的那些成员
同样地,为每个类提供一个等价的构造函数也是不必要的。public eliple(String d,String e,String m)
做的事情与其他所有类似的事情是一样的。注意,类中的逻辑和值基本相同,因此可以更好地设计它们;不要重复你自己
同样,这也适用于您的类方法。它们都做相同的事情;它们从重复的成员中设置或获取值。因此,不要重复它们;使用继承。所有子类也将能够使用这些公共方法。当逻辑真正不同或扩展超类时,您可以覆盖允许cli的方法ent以多态方式调用该方法
此外,需求明确要求使用多态性调用三个方法中的每一个。您还没有这样做;您只调用了toString
。事实上,您无法完成它,因为子类扩展了一个与它没有共同点的超类;您从Shapes
类中一无所获
因此,如果你想在作业中取得好成绩,我建议你复习一下课程材料,再试一次
下面是一个基于一些通用代码的简短示例
public class A {
private String first;
private String second;
public A() {
first = "Hello ";
second = "World";
}
public A(String first, String second) {
this.first = first;
this.second = second;
}
// getters, setters for first and secon
}
public class B extends A {
// THERE'S NO first OR second HERE
// BECAUSE THEY'RE IN A!
private String third;
public B(String first, String second, String third) {
super(first, second);
this.third = third;
}
public void setThird(String third) {
this.third = third;
}
public String getThird() {
return third;
}
// THERE ARE NO GETTERS AND SETTERS HERE FOR
// first AND second BECAUSE THEY'RE IN A!
}
现在,简单代码的惊人使用:
public static void main(String[] args) {
String IMA = "I'm a ";
A[] as = new A[3];
as[0] = new A(IMA, "A");
as[1] = new B(IMA, "B", "third string");
as[2] = new A();
for (A a : as) {
System.out.println(a.getFirst() + a.getSecond());
}
}
在上面的例子中,你不能做的是a.getThird()
;你不能指望a
是B
在我看来,你好像在向各种形状的ctor提供错误的信息。所有参数都是相同的,基本上都是空的(空白).简短回答-不是。您的代码充满了错误,我看不到有意义的多态性演示。除了对派生类具有不同的输出值外,多态性的关键是什么?您指的是“空白”吗作为我的默认构造函数?如果我没有处理我的方法,那就不会给出任何值。我知道如何添加int或double这样的方法来区分每个子类的简单值,比如height,并调用每个值。我的书中没有任何字符串引用polymorphString。我可以将super用于1或2个子类方法和ch为每个形状设置一个字符串输出,但我认为这是偏离目标的。我不确定我是否遵循了你的评论。是的,你可以区分不同类型的形状。一个圆FE应该有一个半径。但是所有形状共有的东西都不应该重复。是的,如果Sub中的方法不同,你应该重写它们,或者扩展基本的bu如果它们是相同的,你就不应该这样做。在它当前的形式中,甚至很难看到子类的使用。我想我可以把它简化为一个单一的形状类,它有一个字符串类型
或类型
,其中类型
是一个枚举
@sjames14这是基本的编程2,我不认为有太多我希望调用半径度量。如果我有编程知识,尤其是将微积分应用到我的程序中,那可能会很好。考虑到我所学的信息,我认为我的情况有点宽松。我更关注如何在我所有的类中使用字符串中的多态性。我认为你没有抓住重点。我不是在暗示你的想法;我只是说形状在不同的方面是不同的,但它们也是一样的。我试图让你明白,你可能担心使用多态性,但你做得不对。请参阅我最近的编辑。@sjames4,我建议你调试代码,并注意正方形
会有重复的字段。它会有自己的移动
,擦除
,和绘制
字符串
和一个矩形。移动
,矩形。擦除
,矩形。绘制
字符串s。它们不一样。