【问题描述】
显示一张桌子的信息,包括桌子的形状(长方形、方形、圆形、椭圆形;使用Rect、Square、Circle、Ellipse)、腿数、高度、桌面面积。定义变量来保存桌子的信息,并显示各个信息的值。要点提示:
1)显示桌子信息:形状、腿数、高度、面积
2)注意各个变量的数据类型,桌子的形状可以用字符串String来存储。
【输入形式】
输入桌子各个属性的值。
【输出形式】
按照指定格式输出各个属性的值。
【运行截图】
import java.util.Scanner;
public class Table {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
int leg=sc.nextInt();
int hight=sc.nextInt();
int ar=sc.nextInt();
Table ta=new Table(s,leg,hight,ar);
System.out.println(ta);
sc.close();
}
}
插入代码块为:
String shape;
int leg;
int Hight;
int Area;
@Override
public String toString() {
return
"Shape:" + shape + "n"+
"Legs:" + leg +"n"+
"Hight" + Hight +"n"+
"Area:" + Area ;
}
public Table(String shape, int leg, int Hight, int Area) {
this.shape = shape;
this.leg = leg;
this.Hight = Hight;
this.Area = Area;
}
@Override
为注释下面函数为重写,就相当于一个提醒注释的作用
toString方法在Object类中定义,因为Java中每个类都默认继承Object类,所以每个类都具有toString方法,作用是返回对象的字符串表示(类名+符号@+对象的哈希码)。
为了使返回值更有意义,所以常用的类都已经重写了toString方法,如String类、Date类。
我们自己写的类也建议重写toString方法。
println(类名.toStirng) <==>println(类名)
原因跳转链接:
https://blog.csdn.net/Echo_width/article/details/79711540?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165218635916781483732475%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=165218635916781483732475&biz_id=0&spm=1018.2226.3001.4187



