A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 姚伟涛 中级黑马   /  2012-4-12 20:11  /  1986 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public interface Printx {
  2.    public void printMyWay();
  3. }
  4. public class Squre implements Printx{
  5.     double height,wide;
  6.     double area,length;
  7.     Squre(double height,double wide){
  8.         this.height=height;
  9.         this.wide=wide;
  10.     }
  11.     Squre(double height){
  12.         this.height=height;
  13.     }
  14.     public void printMyWay(){
  15.         area=height*wide;
  16.         length=2*(height+wide);   
  17.     };   
  18. }
  19. public class Squreson extends Squre {
  20.     double diagonal;
  21.     Squreson(double height){
  22.         this.height=height;
  23.     }
  24.     public void printMyWay(){
  25.         area=height*height;
  26.         length=4*height;
  27.         diagonal=sqrt(2*height*height);
  28.     }   
  29.     }
  30. }
  31. public class Print {
  32.     public static void main(String[] args) {
  33.         Squre squre;
  34.         Squreson squreson;
  35.         squre=new Squre(4,5);
  36.         squre.printMyWay();
  37.         squreson=new Squreson(4);
  38.         squreson.printMyWay();
  39.     }
  40. }
  41. 上面的编译出现了3个错误:
  42. 符号: 构造函数 Squreson(int)
  43. 位置: 类 接口.Squreson
  44.         squreson=new Squreson(4);
  45. 符号: 构造函数 Squre()
  46. 位置: 类 接口.Squre
  47. public class Squreson extends Squre {
  48. 符号: 方法 sqrt(double)
  49. 位置: 类 接口.Squreson
  50.         diagonal=sqrt(2*height*height);
  51. 请问是什么问题呢?
复制代码

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

9 个回复

倒序浏览
本帖最后由 赵孟恩 于 2012-4-12 21:16 编辑
  1. public interface Printx {

  2.    public void printMyWay();

  3. }

  4. public class Squre implements Printx{

  5.     double height,wide;

  6.     double area,length;

  7.     Squre(double height,double wide){

  8.         this.height=height;

  9.         this.wide=wide;

  10.     }

  11.     Squre(double height){

  12.         this.height=height;

  13.     }

  14.     public void printMyWay(){

  15.         area=height*wide;

  16.         length=2*(height+wide);   

  17.     };                                                //这多了;
  18. }

  19. public class Squreson extends Squre {

  20.     double diagonal;

  21.     Squreson(double height){

  22.         this.height=height;                       // 子类的构造方法必须先调用父类的构造方法  改成 super(height);
  23.     }

  24.     public void printMyWay(){

  25.         area=height*height;

  26.         length=4*height;

  27.         diagonal=sqrt(2*height*height);           // 应该是Math.sqrt(2*height*height);

  28.     }   

  29.     }                                                    //这多了大括号

  30. }

  31. public class Print {

  32.     public static void main(String[] args) {

  33.         Squre squre;

  34.         Squreson squreson;

  35.         squre=new Squre(4,5);

  36.         squre.printMyWay();

  37.         squreson=new Squreson(4);

  38.         squreson.printMyWay();

  39.     }

  40. }

  41. 上面的编译出现了3个错误:

  42. 符号: 构造函数 Squreson(int)        //这个错误没发现啊

  43. 位置: 类 接口.Squreson

  44.         squreson=new Squreson(4);

  45. 符号: 构造函数 Squre()

  46. 位置: 类 接口.Squre

  47. public class Squreson extends Squre {

  48. 符号: 方法 sqrt(double)

  49. 位置: 类 接口.Squreson

  50.         diagonal=sqrt(2*height*height);
  51. 要是代码一起编译也会出些错误, Print.java文件名  其他类或接口不用public修饰
复制代码

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

回复 使用道具 举报
第一 。你没写父类午餐的构造函数 。。子类在继承时也没写调用有参数的。。Squreson(double height) 接收double值。。你给的4是int。。除非你写4.0;;第三你的sqrt是什么东西。。哪里来的。。

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

回复 使用道具 举报
  1. <FONT color=red>import java.lang.StrictMath;
  2. </FONT>interface Printx {
  3. public void printMyWay();
  4. }
  5. class Squre implements Printx{
  6. double height,wide;
  7. double area,length;
  8. Squre(double height,double wide){
  9. this.height=height;
  10. this.wide=wide;
  11. }
  12. Squre(double height){
  13. this.height=height;
  14. }
  15. public void printMyWay(){
  16. area=height*wide;
  17. length=2*(height+wide);
  18. };
  19. }
  20. class Squreson extends Squre {
  21. double diagonal;
  22. Squreson(double height)
  23. {
  24. super(height);
  25. <FONT color=red>//因为父类的空参数构造函数已经自动消失,这边一定要手动指定一个父类的构造函数引用;
  26. //this.height=height;
  27. </FONT>}
  28. public void printMyWay(){
  29. area=height*height;
  30. length=4*height;
  31. diagonal=StrictMath.sqrt(2*height*height);
  32. <FONT color=red>//这边我也不大明白,我查了一下API,想要使用static修饰符所修饰的sqrt(),必须要导入java.lang.StrictMath包或者java.lang.Math包;
  33. //最后还要提一下,一个java文件只能有一个公共类,而且这个公共类的类名要后java文件名一致;</FONT>
  34. }
  35. }
  36. class Print {
  37. public static void main(String[] args) {
  38. Squre squre;
  39. Squreson squreson;
  40. squre=new Squre(4,5);
  41. squre.printMyWay();
  42. squreson=new Squreson(4);
  43. squreson.printMyWay();
  44. }
  45. }
复制代码
上面的红色字体为我所改动的部分,自己看一下吧!

还有提个小建议,你以后写代码的时候尽量把该标识的地方标识一下,这样提高代码的阅读性

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

回复 使用道具 举报

  1. <P> </P>
复制代码
  1. <FONT color=red>import java.lang.StrictMath;
  2. </FONT>interface Printx {
  3. public void printMyWay();
  4. }
  5. class Squre implements Printx{
  6. double height,wide;
  7. double area,length;
  8. Squre(double height,double wide){
  9. this.height=height;
  10. this.wide=wide;
  11. }
  12. Squre(double height){
  13. this.height=height;
  14. }
  15. public void printMyWay(){
  16. area=height*wide;
  17. length=2*(height+wide);
  18. };
  19. }
  20. class Squreson extends Squre {
  21. double diagonal;
  22. Squreson(double height)
  23. {
  24. super(height);
  25. <FONT color=red><FONT color=red>//因为父类的空参数构造函数已经自动消失,这边一定要手动指定一个父类的构造函数引用;
  26. //this.height=height;
  27. </FONT>}
  28. </FONT>public void printMyWay(){
  29. area=height*height;
  30. length=4*height;
  31. diagonal=StrictMath.sqrt(2*height*height);
  32. <FONT color=red><FONT color=red>//这边我也不大明白,我查了一下API,想要使用static修饰符所修饰的sqrt(),必须要导入java.lang.StrictMath包或者java.lang.Math包;
  33. //最后还要提一下,一个java文件只能有一个公共类,而且这个公共类的类名要后java文件名一致;</FONT></FONT>
  34. }
  35. }
  36. class Print {
  37. public static void main(String[] args) {
  38. Squre squre;
  39. Squreson squreson;
  40. squre=new Squre(4,5);
  41. squre.printMyWay();
  42. squreson=new Squreson(4);
  43. squreson.printMyWay();
  44. }
  45. }
复制代码
回复 使用道具 举报
super(height);

<FONT color=red>//因为父类的空参数构造函数已经自动消失,这边一定要手动指定一个父类的构造函数引用;

//this.height=height;

</FONT>}

public void printMyWay(){

area=height*height;

length=4*height;

diagonal=StrictMath.sqrt(2*height*height);

<FONT color=red>//这边我也不大明白,我查了一下API,想要使用static修饰符所修饰的sqrt(),必须要导入java.lang.StrictMath包或者java.lang.Math包;

//最后还要提一下,一个java文件只能有一个公共类,而且这个公共类的类名要后java文件名一致;</FONT>

}

}

class Print {


不知道,标注红色的,怎么没有显示,上面我引用的部分,就是标注的地方
回复 使用道具 举报
package com.wd;
import java.math.*;
interface Printinfor{
    public void printMyWay();
}
class Squre implements Printinfor
{
     double height,wide;
     double area,length;
     Squre(double height,double wide){
         this.height=height;
         this.wide=wide;
     }
     Squre(double height){
         this.height=height;
     }
     public void printMyWay(){
         area=height*wide;
         length=2*(height+wide);   
     };   
}
class Squreson extends Squre {
  double diagonal;
     Squreson(double height) {
   super(height);
   // TODO Auto-generated constructor stub
  }

  /*   Squreson(double height){
      
         super.height=height;
     }
*/
     public void printMyWay(){
         area=height*height;
         length=4*height;
         diagonal=Math.sqrt(2*height*height);
     }   

}
public class Print {
     public static void main(String[] args) {
         Squre squre;
         Squreson squreson;
         squre=new Squre(4,5);
         squre.printMyWay();
         squreson=new Squreson(4);
         squreson.printMyWay();
     }
}
上面的编译可以成功运行
有几个问题不明白!
1.public interface Printinfor{
    public void printMyWay();
}
如果保留public 关键字
在eclipse中提示 "类 Printinfor 是公共的,应在名为 Printinfor.java 的文件中声明",如果保留public 那么如何在Printinfor中声明
2.
   Squreson(double height){
      
         super.height=height;
     }
在原来的程序中改为上述,使用super,为什么不可以

如有解答,不胜感激哈!

回复 使用道具 举报
武庆东 发表于 2012-4-12 21:33
package com.wd;
import java.math.*;
interface Printinfor{

1  可以将public修饰的类单独Printinfor.java

2 子类构造方法必须要调用父类的构造方法,你这没调用啊
回复 使用道具 举报
蒋亮 中级黑马 2012-4-13 00:31:26
9#
1、你的接口定义就有问题,接口中只能有抽象方法,应改为public abstract void printMyWay();
2、一个.java文件中只能有一个public类,你这里有三个,显然不对;
3、sqrt是Math类的静态方法,你应该使用类名来调用,如Math.sqrt()或者静态导入;
4、squreson=new Squreson(4);你在构造方法中定义height是浮点型,但是你在创建对象的时候传进去的是一个整型,所以错,你应该传入浮点型,如4.0;
先就这么多吧,我勒个去啊,是不是真的要技术分25才能申请黑马训练营啊,我才2分,肿么搞啊。。。。
回复 使用道具 举报
赵孟恩 发表于 2012-4-12 23:24
1  可以将public修饰的类单独Printinfor.java

2 子类构造方法必须要调用父类的构造方法,你这没调用啊 ...

2 子类构造方法必须要调用父类的构造方法,你这没调用啊..
   子类复写了父类的构造方法!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马