关于多态和接口的题

advanced676 2009-11-04 10:47:48
本人是菜鸟,刚开始接触java,还有很多不懂呢,请问下这两题怎么做啊
1,编写一个类继承的例子,理解继承状态下的初始化顺序,使得类初始化后得到如下输出
parent:j=1;
child:b=10;
parent:j=3;
child:a=10;

2,定义一个矩形类,在派生出一个正方形类,自行扩充成员变量和方法,
再定义一个接口EqualDiagonal,其中包含方法getDiagonal,在矩形和正方
形中实现此接口
...全文
169 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
bawgiitx 2009-11-06
  • 打赏
  • 举报
回复
bawgiitx 2009-11-06
  • 打赏
  • 举报
回复
还没解决呀,可怜的娃,初始化顺序看这个,这个代码就不用写了Java中类的初始化顺序

1

class parent {
parent() {
j+=2;
}
public static int j = -1;
}

class child extends parent {
public int b;
child(int b) {
this.b=b;
}
}

public class Main {
public static void main(String[] args) throws Exception {
child b = new child(10);
System.out.println("parent:j="+b.j);//这里打印1是因为child(10)时,其父类parent()执行一次构造函数,静态变量j=j+2,j的值就成了1
System.out.println("child:b="+b.b);//这里是child(10)传进的10
child a = new child(10);
System.out.println("parent:j="+a.j);//这里打印的是3同上,其父类parent()执行一次构造函数,静态变量j=j+2,j的值就成了3
System.out.println("child:a="+a.b);
}
}
bawgiitx 2009-11-05
  • 打赏
  • 举报
回复
1

class child{
child(){
System.out.println("child:b=10");
}
}
class parent extends child{

parent(int j) {
System.out.println("parent:j="+j);
}
}
public class Main {
public static void main(String[] args) throws Exception {
new parent(1);
new parent(3);
}
}
advanced676 2009-11-05
  • 打赏
  • 举报
回复
自己up up
advanced676 2009-11-05
  • 打赏
  • 举报
回复
高手帮下忙啊
advanced676 2009-11-05
  • 打赏
  • 举报
回复
还是不行,输出的是:
parent:j=1
child:b=10
parent:j=3
child:b=10
要求是:
parent:j=1;
child:b=10;
parent:j=3;
child:a=10;
最后不同
luffyke 2009-11-05
  • 打赏
  • 举报
回复

public class Test091105_parent {

public Test091105_parent(){

}
public Test091105_parent(int i){
System.out.println("parent:i="+i);
}
public class Test091105_child extends Test091105_parent{

public Test091105_child(int i,char j,int k) {
super(i);
System.out.println("child:"+j+"="+k);
}
public Test091105_child(){

}
public class Test091105_main {

public static void main(String[] args) {

new Test091105_child(1,'b',10);
new Test091105_child(3,'a',10);
}
}

bawgiitx 2009-11-05
  • 打赏
  • 举报
回复
2

interface EqualDiagonal {
double getDiagonal();
}

class Rectangle implements EqualDiagonal {//矩形

double w;
double h;

public Rectangle() {
w = h = 0;
}

public void setW(double w) throws Exception {
if (w < 0) {
throw new Exception("");

}
this.w = w;
}

public void setH(double h) throws Exception {
if (h < 0) {
throw new Exception("");

}
this.h = h;
}

public double getDiagonal() {
return Math.sqrt(w * w + h * h);
}
}

class square extends Rectangle {

public boolean is_square;

square() {
}

public boolean isSquare() {
is_square = (w == h);
return is_square;
}

@Override
public double getDiagonal() {
if (isSquare()) {
return Math.sqrt(2 * w * w);
}
return 0;
}
}

public class NewMain {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
float w = 3, h = 4;
Rectangle r = null;
try {
r = new Rectangle();
r.setH(3);
r.setW(4);
} catch (Exception ex) {
}
System.out.println("" + r.getDiagonal());
square rr = new square();
try {
rr.setH(5);
rr.setW(5);
} catch (Exception ex) {
}
System.out.println("is square: "+rr.isSquare());
System.out.println("" + rr.getDiagonal());
}
}
bawgiitx 2009-11-05
  • 打赏
  • 举报
回复
输出的地方没改回来

class parent{
parent(){
}
parent(int b){
System.out.println("parent:b="+b);
}
}
class child extends parent{

child(int j,int b) {
super(b);
System.out.println("child:j="+j);
}
}
public class Main {
public static void main(String[] args) throws Exception {
new child(10,1);
new child(10,3);
}
}
bawgiitx 2009-11-05
  • 打赏
  • 举报
回复
~~~~~~~~~~~~

class parent{
parent(){
}
parent(int b){
System.out.println("parent:b="+b);
}
}
class child extends parent{

child(int j,int b) {
super(b);
System.out.println("child:j="+j);
}
}
public class Main {
public static void main(String[] args) throws Exception {
new child(10,1);
new child(10,3);
}
}
advanced676 2009-11-05
  • 打赏
  • 举报
回复
错了
输出是:
child:b=10
parent:j=1
child:b=10
parent:j=3
advanced676 2009-11-05
  • 打赏
  • 举报
回复
楼上的好像错了!输出结果是
parent:j=1
child:b=10
parent:j=1
child:b=3
advanced676 2009-11-04
  • 打赏
  • 举报
回复
大家帮帮忙啊!

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧