最近开始学java,讲师布置了有些题 还有自己找的题做做,都是自己写的 放上来 求高手指教 不论是规范还是算法 还是习惯什么的只要是您觉着不顺眼就说一下所有人都是我的老师 希望大家不吝赐教 讲到继承了 于是这么一个题 定义个矩形 然后给值 判断是长方形 还是正方形 然后计算面积和周长
public class Rectangle {
public static void main(String[] args) {
Square s = new Square();
s.ifRect();
System.out.print("面积是" + s.calculateArea());
System.out.print(",周长是" + s.calculateGirth());
}
}class Rect {
int width;
int heigth;
public Rect() {
}
//无参的构造方法
public Rect(int w, int h) {
this.width = w;
this.heigth = h;
}
//有参的构造方法
public void ifRect() {
if (this.width == this.heigth) {
System.out.print("这是一个正方形:");
}
if (this.width != this.heigth) {
System.out.print("这是一个长方形:");
}
}
public int calculateArea() {
return this.width * this.heigth;
}
//计算长方形的计算面积方法
public int calculateGirth() {
return 2 * (this.width + this.heigth);
}
}class Square extends Rect {
public Square() {
super(3,3);
}
}
public class Rectangle {
public static void main(String[] args) {
Square s = new Square();
s.ifRect();
System.out.print("面积是" + s.calculateArea());
System.out.print(",周长是" + s.calculateGirth());
}
}class Rect {
int width;
int heigth;
public Rect() {
}
//无参的构造方法
public Rect(int w, int h) {
this.width = w;
this.heigth = h;
}
//有参的构造方法
public void ifRect() {
if (this.width == this.heigth) {
System.out.print("这是一个正方形:");
}
if (this.width != this.heigth) {
System.out.print("这是一个长方形:");
}
}
public int calculateArea() {
return this.width * this.heigth;
}
//计算长方形的计算面积方法
public int calculateGirth() {
return 2 * (this.width + this.heigth);
}
}class Square extends Rect {
public Square() {
super(3,3);
}
}
Square s = new Square();//这里直接定义为正方形,然后再判断是什么形状,没什么意义方法的注释建议放在定义方法的上面一行
//有参的构造方法
public void ifRect() {
if (this.width == this.heigth) {
System.out.print("这是一个正方形:");
}
if (this.width != this.heigth) {
System.out.print("这是一个长方形:");
}
}
你可以修改下:不提倡在类中直接用System.out.println()//有参的构造方法
public boolean isRect() {
if (this.width == this.heigth) {
return ture;
}
if (this.width != this.heigth) {
return false;
}
}
我自己总结下哈
1;注释的问题 放在方法上面
2;成员变量的修饰 最好要用private
3;变量的命名那意思是要见名知意哈
4;尽量不要在类中直接用system.out.print 要有返回值
5;还是命名的问题吧 包括方法名 类名 最好看到就知道什么意思哈 是不?
才学了一个多星期 大家多给点意见 说点不足 别讽刺我哈 谁都有这一步 各位客官是不是啊?
我在这里把代码贴出来就是想知道哪里有问题,这样你们给我说了 我下次就会注意 这是一个进步的过程不是吗
谢谢给我说名病的这些人
上课卡壳有木有 自己讲着讲着不回了有木有
根本找不到她的思路有木有
我悲剧了有木有
java注释方法网上有很多,自己百度一下,java编码规范网上也有!多看看,多练练就好了!