Java父类和子类

jaraice 2018-10-23 05:19:25
我想问问,就是我在子类中计算出来的数值我怎么才能调用到父类去输出呢?就是我在几个子类中算出一些图形的面积,然后需要到父类主函数计算总和,该用什么方法?
...全文
629 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
泡泡先生 2018-11-16
  • 打赏
  • 举报
回复
这问的我们一脸蒙蔽啊; public static void main(String args[]){ Son son= new Son (); int sum = son.getSum(); System.out.println("sum=" + sum); }
weixin_41916899 2018-11-16
  • 打赏
  • 举报
回复
引用 6 楼 aaa11111232的回复:
直接用supet.方法名()这样都不行?
super吧
量力而行 2018-11-07
  • 打赏
  • 举报
回复
学习学习,前面的几个办法我都验证了一下
aaa11111232 2018-10-28
  • 打赏
  • 举报
回复
直接用supet.方法名()这样都不行?
楓VS痕 2018-10-24
  • 打赏
  • 举报
回复
引用 3 楼 whyyouhitme_ 的回复:
借楼问一下,maven构建索引按照视频一步一步做,结果却不一样,图1是教程,图2 是我自己的


maven会根据pom.xml去下载jar到仓库,仓库没有jar,说明你项目的pom.xml没有配置,或者仓库配置的下载地址不对吧
whyyouhitme_ 2018-10-24
  • 打赏
  • 举报
回复
借楼问一下,maven构建索引按照视频一步一步做,结果却不一样,图1是教程,图2 是我自己的
whyyouhitme_ 2018-10-24
  • 打赏
  • 举报
回复
引用 4 楼 xiewu91124 的回复:
[quote=引用 3 楼 whyyouhitme_ 的回复:]
借楼问一下,maven构建索引按照视频一步一步做,结果却不一样,图1是教程,图2 是我自己的


maven会根据pom.xml去下载jar到仓库,仓库没有jar,说明你项目的pom.xml没有配置,或者仓库配置的下载地址不对吧[/quote]
解决啦!谢谢,但是新建maven的时候pom.xml报错,用了java ee tools 生成web.xml也不行。而且pom打开后也不是xml文件,而是个网页一样的东西,报错如下:Failure to transfer org.apache.maven:maven-parent:pom:9 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven:maven-parent:pom:9 from/to central (https://repo.maven.apache.org/maven2): The operation was cancelled.
zhangphh 2018-10-23
  • 打赏
  • 举报
回复
子类和父类是继承中的叫法,这种计算图形面积的类应该是工具类
楓VS痕 2018-10-23
  • 打赏
  • 举报
回复
public class Father {

public int getSum(){
Son son = new Son();
int acreage1 = son.getAcreage1();
int acreage2 = son.getAcreage2();
int acreage3 = son.getAcreage3();

return acreage1 + acreage2+acreage3;
}

public static void main(String args[]){
Father father = new Father();
int sum = father.getSum();
System.out.println("sum=" + sum);
}
}


public class Son extends Father {

public int getAcreage1() {
int acreage1 = 10;
return acreage1;
}

public int getAcreage2() {
int acreage2 = 15;
return acreage2;
}

public int getAcreage3() {
int acreage3 = 22;
return acreage3;
}
}

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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