|
查看: 1179|回复: 13
|
Java code inheritance
[复制链接]
|
|
|
我又来了,想问问大家哦!我是做到答案出来了。可是偏偏它不会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的呢? |
|
|
|
|
|
|
|
|
|
|
发表于 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。
我把你的程式改寫,你參考看看。
- class abstract Shape
- {
- int width;
- int height;
-
- public Shape(int width, int height)
- {
- this.width = width;
- this.height = height;
- }
-
- public int getArea()
- {
- return width * height;
- }
-
- public abstract void printArea();
- }
- class Square extends Shape
- {
- public Square(int length)
- {
- super(length, length);
- }
-
- @Override
- public void printArea()
- {
- System.out.println("The area of square is " + getArea());
- }
- }
- class Rectangle extends Shape
- {
- public Rectangle(int width, int height)
- {
- super(width, height);
- }
-
- @Override
- public void printArea()
- {
- System.out.println("The area of rectangle is " + getArea());
- }
- }
- public class Test
- {
- public static void main(String[] args)
- {
- new Square(5).printArea();
- new Rectangle(10, 50).printArea();
- }
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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了,囧。
- class Shape
- {
- int width;
- int height;
-
- public Shape(int width, int height)
- {
- this.width = width;
- this.height = height;
- }
-
- public int getArea()
- {
- return width * height;
- }
-
- public void printArea()
- {
- System.out.println("The area of shape is " + getArea());
- }
- }
复制代码
[ 本帖最后由 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 @@ |
|
|
|
|
|
|
|
|
|
|
发表于 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 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 29-3-2009 01:03 PM
|
显示全部楼层
我不知道initialization list,也不會C++,我猜你的意思大概是這樣
- class Shape
- {
- int width, height;
- public Shape()
- {
- this(4, 5);
- }
- public Shape(int width, int height)
- {
- this.width = width;
- this.height = height;
- }
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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();
}
} |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|