佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1179|回复: 13

Java code inheritance

[复制链接]
发表于 26-3-2009 09:50 PM | 显示全部楼层 |阅读模式
我又来了,想问问大家哦!我是做到答案出来了。可是偏偏它不会output之前的资料的呢?
class Shape1{
float width;
float height;
float size;
float are,squ;

Shape1(){
}
Shape1(float w, float h){
  width=w;
  height=h;
  
}
void area(){
  //are=width*height;
  //squ=width*height;
System.out.println("the area of rectangle " + width*height);
  
}
void area2(){
  System.out.println("the area of square "+ width*height);
}
}
class Rectangle1 extends Shape1{
Rectangle1(){
}
Rectangle1(float w, float h){
  super(w,h);
}
public void printrectangle(){
  Rectangle1 r1=new Rectangle1(10,50);
  System.out.println("width of rectangle  " + width);
  System.out.println("height of rectangle " +height);
  r1.area();
  //System.out.println("the area of rectangle " +are);
}
}
class Square extends Shape1{
Square(){
}
Square(float w,float h){
  super(w,h);
}
public void printsquare(){
  Square s1=new Square(5,5);
  System.out.println("Size of square  " + size);
  s1.area2();
  //System.out.println("the area of square " + squ);
}
}
public class ResultRecandSquare{
public static void main(String args[]){
  new Rectangle1().printrectangle();
  new Square().printsquare();
}
}


output:





为什么会是0的呢?
回复

使用道具 举报


ADVERTISEMENT

发表于 26-3-2009 09:59 PM | 显示全部楼层
原帖由 科技小女孩 于 26-3-2009 09:50 PM 发表
我又来了,想问问大家哦!我是做到答案出来了。可是偏偏它不会output之前的资料的呢?
public void printrectangle(){
  Rectangle1 r1=new Rectangle1(10,50);
  System.out.println("width of rectangle  " + width);
  System.out.println("height of rectangle " +height);
  r1.area();
  //System.out.println("the area of rectangle " +are);
}

我已经N年没动java了
你把value 用去create 新的object, 本身的variable 都没改到,当然是0 (default value)啦
回复

使用道具 举报

发表于 27-3-2009 01:46 AM | 显示全部楼层
正方形是等邊,所以只要丟一個參數給它就好。繼承的基本觀念就是先找出各物件相同的屬性做成super class,剩下要customize的部分再另外寫一個class去繼承super class。
我把你的程式改寫,你參考看看。

  1. class abstract Shape
  2. {
  3.         int width;
  4.         int height;
  5.        
  6.         public Shape(int width, int height)
  7.         {
  8.                 this.width = width;
  9.                 this.height = height;
  10.         }
  11.        
  12.         public int getArea()
  13.         {
  14.                 return width * height;
  15.         }
  16.        
  17.         public abstract void printArea();
  18. }

  19. class Square extends Shape
  20. {
  21.         public Square(int length)
  22.         {
  23.                 super(length, length);
  24.         }
  25.        
  26.         @Override
  27.         public void printArea()
  28.         {
  29.                 System.out.println("The area of square is " + getArea());
  30.         }
  31. }

  32. class Rectangle extends Shape
  33. {
  34.         public Rectangle(int width, int height)
  35.         {
  36.                 super(width, height);
  37.         }
  38.        
  39.         @Override
  40.         public void printArea()
  41.         {
  42.                 System.out.println("The area of rectangle is " + getArea());
  43.         }
  44. }

  45. public class Test
  46. {
  47.         public static void main(String[] args)
  48.         {
  49.                 new Square(5).printArea();
  50.                 new Rectangle(10, 50).printArea();
  51.         }
  52. }
复制代码
回复

使用道具 举报

 楼主| 发表于 27-3-2009 10:12 PM | 显示全部楼层
原帖由 MaokeJackson 于 27-3-2009 01:46 AM 发表
正方形是等邊,所以只要丟一個參數給它就好。繼承的基本觀念就是先找出各物件相同的屬性做成super class,剩下要customize的部分再另外寫一個class去繼承super class。
我把你的程式改寫,你參考看看。

class ab ...
.phot

output is like this? got error
回复

使用道具 举报

发表于 27-3-2009 11:09 PM | 显示全部楼层
忘記abstract不可以有constructor了,囧。
  1. class Shape
  2. {
  3.         int width;
  4.         int height;
  5.       
  6.         public Shape(int width, int height)
  7.         {
  8.                 this.width = width;
  9.                 this.height = height;
  10.         }
  11.       
  12.         public int getArea()
  13.         {
  14.                 return width * height;
  15.         }
  16.       
  17.         public void printArea()
  18.         {
  19.                 System.out.println("The area of shape is " + getArea());
  20.         }
  21. }
复制代码

[ 本帖最后由 MaokeJackson 于 27-3-2009 11:12 PM 编辑 ]
回复

使用道具 举报

发表于 28-3-2009 09:56 PM | 显示全部楼层
原帖由 MaokeJackson 于 27-3-2009 11:09 PM 发表
忘記abstract不可以有constructor了,囧。
class Shape
{
        int width;
        int height;
      
        public Shape(int width, int height)
        {
                this.width = width;
...


java 没有constructor initialization list 吗?为什么要在constructor 里做 assignment @@
回复

使用道具 举报

Follow Us
发表于 29-3-2009 12:58 AM | 显示全部楼层
原帖由 yeenfei 于 28-3-2009 09:56 PM 发表
java 没有constructor initialization list 吗?为什么要在constructor 里做 assignment @@

要hard code也不是不可以,只是個人習慣這樣做。
回复

使用道具 举报

发表于 29-3-2009 11:53 AM | 显示全部楼层
原帖由 MaokeJackson 于 29-3-2009 12:58 AM 发表

要hard code也不是不可以,只是個人習慣這樣做。

为什么hardcode呢? 我讲的initialization list如果在c++ 大概是这样

class Shape
{
  int m_width;
  int m_height;
   
public:
  Shape(int width, int height) :
  m_width (width),
  m_height (height)

  {
  }

}

[ 本帖最后由 yeenfei 于 29-3-2009 11:55 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 29-3-2009 01:03 PM | 显示全部楼层
我不知道initialization list,也不會C++,我猜你的意思大概是這樣
  1. class Shape
  2. {
  3.     int width, height;
  4.     public Shape()
  5.     {
  6.         this(4, 5);
  7.     }

  8.     public Shape(int width, int height)
  9.     {
  10.         this.width = width;
  11.         this.height = height;
  12.     }
  13. }
复制代码
回复

使用道具 举报

 楼主| 发表于 1-4-2009 03:20 PM | 显示全部楼层
那应该要怎样写呢?我要show回出来
rectangle and square的width and height
回复

使用道具 举报

 楼主| 发表于 1-4-2009 03:24 PM | 显示全部楼层

回复 10# 科技小女孩 的帖子

那应该要怎样写呢?我要show回出来
rectangle and square的width and height
回复

使用道具 举报

发表于 1-4-2009 04:40 PM | 显示全部楼层
在 Shape1 加上这两个。。
public float getWidth() {
        return width;
    }

    public float getHeight() {
        return height;
    }

然后这里换成这样
public void printrectangle() {
        Rectangle1 r1 = new Rectangle1(10, 50);
        System.out.println("width of rectangle  " + r1.getWidth());
        System.out.println("height of rectangle " + r1.getHeight());
        r1.area();
        //System.out.println("the area of rectangle " +are);
    }

试试看
回复

使用道具 举报

 楼主| 发表于 1-4-2009 05:34 PM | 显示全部楼层
原帖由 r9jason 于 1-4-2009 04:40 PM 发表
在 Shape1 加上这两个。。
public float getWidth() {
        return width;
    }

    public float getHeight() {
        return height;
    }

然后这里换成这样
public void printrectangle() {
...

还是不能,你能写整个给我看看吗?
回复

使用道具 举报

发表于 1-4-2009 07:28 PM | 显示全部楼层
class Shape1 {
    float width;
    float height;
    float size;
    float are, squ;

    Shape1() {
    }

    Shape1(float w, float h) {
        width = w;
        height = h;

    }

    public float getWidth() {
        return width;
    }

    public float getHeight() {
        return height;
    }

    public float getSize() {
        return width * height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    void area() {
        //are=width*height;
        //squ=width*height;
        System.out.println("the area of rectangle " + width * height);

    }

    void area2() {
        System.out.println("the area of square " + width * height);
    }
}

class Rectangle1 extends Shape1 {
    Rectangle1() {
    }

    Rectangle1(float w, float h) {
        super(w, h);
    }

    public void printrectangle() {
        Rectangle1 r1 = new Rectangle1(10, 50);
        System.out.println("width of rectangle  " + r1.getWidth());
        System.out.println("height of rectangle " + r1.getHeight());
        r1.area();
        //System.out.println("the area of rectangle " +are);
    }
}

class Square extends Shape1 {
    Square() {
    }

    Square(float w, float h) {
        super(w, h);
    }

    public void printsquare() {
        Square s1 = new Square(5, 5);
        System.out.println("Size of square  " + s1.getSize());
        s1.area2();
        //System.out.println("the area of square " + squ);
    }
}

public class ResultRecandSquare {
    public static void main(String args[]) {
        new Rectangle1().printrectangle();
        new Square().printsquare();
    }
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 14-12-2025 07:58 PM , Processed in 0.116605 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表