网站首页 > 文章中心 > 其它

继承代码java

作者:小编 更新时间:2023-08-16 10:01:22 浏览量:324人看过

写一个最简单的JAVA继承代码?谢谢

可运行的:

import java.awt.*;

import java.awt.event.*;

public class BackJFrame extends Frame{

public BackJFrame(){

super("台球");

setBackground(Color.cyan); //背景

setVisible(true);

addWindowListener(new WindowAdapter()

{

public void windowClosing (WindowEvent e)

{System.exit(0);}

} );

}

public static void main(String args[]){

new BackJFrame();

Java中是如何通过继承实现代码重用的?

措施一:改写类的实例方法

通过类继承实现代码重用不是精确的代码重用技术,所以呢它并不是最理想的代码重用机制.换句话说,如果不继承整个类的所有方法和数据成员,我们无法重用该类里面的单个方法.继承总是带来一些多余的方法和数据成员,它们总是使得重用类里面某个方法的代码复杂化.另外,派生类对父类的依赖关系也使得代码进一步复杂化:对父类的改动可能影响子类;修改父类或者子类中的任意一个类时,我们很难记得哪一个方法被子类覆盖、哪一个方法没有被子类覆盖;最后,子类中的覆盖方法是否要调用父类中的对应方法有时并不显而易见.

任何方法,只要它执行的是某个单一概念的任务,就其本身而言,它就应该是首选的可重用代码.为了重用这种代码,我们必须回归到面向过程的编程模式,把类的实例方法移出成为全局性的过程.为了提高这种过程的可重用性,过程代码应该象静态工具方法一样编写:它只能使用自己的输入参数,只能调用其他全局性的过程,不能使用任何非局部的变量.这种对外部依赖关系的限制简化了过程的应用,使得过程能够方便地用于任何地方.当然,由于这种组织方式总是使得代码具有更清晰的结构,即使是不考虑重用性的代码也同样能够从中获益.

java编写动物世界的继承关系代码

我写了一个,内容比较简单的.代码如下:public class AnimalTest {

Animal animal;

public void eat(Animal animal){

animal.eat();

public void walk(Animal animal){

animal.walk();

Animal animal=new Animal("animal");

Wolf w=new Wolf("wolf");

Goat g=new Goat("goat");

AnimalTest at=new AnimalTest();

at.eat(animal);

at.eat(w);

at.eat(g);

at.walk(animal);

at.walk(w);

at.walk(g);

class Animal {

String name;

public Animal(String name){

this.name=name;

public Animal(){}

public void setName(String name){

public String getName(){

return name;

public void eat(){

System.out.println("animal eat");

public void walk(){

System.out.println("animal walk");

public String toString(){

}class Wolf extends Animal {

public Wolf(String name){

super(name);

public Wolf(){}

System.out.println("wolf eat meat");

System.out.println("wolf walk");

}class Goat extends Animal {

public Goat(String name){

public Goat(){}

System.out.println("goat eat grass");

System.out.println("goat walk");

JAVA继承问题 求代码

第一个:

public?class?Yaojing?{

protected?String?name;

protected?int?age;

protected?String?gender;

public?void?showBasicInfo()?{

System.out.println(toString());

public?void?eatTangSeng()?{

System.out.println("吃饱了");

@Override

public?String?toString()?{

return?"Yaojing?[name="?◆?name?◆?",?age="?◆?age?◆?",?gender="?◆?gender?◆?"]";

第二个类

public?class?Zhizhujing?extends?Yaojing?{

public?void?buildNet(){

System.out.println("蜘蛛在织网");

第三个类

public?class?Baigujing?extends?Yaojing?{

public?void?beBeauty(){

System.out.println("白骨精");

java程序继承

package?extend;

/**

*?圆类

*/

public?class?Circle?{

private?double?r;

public?Circle(double?r)?{

this.r?=?r;

public?double?Circumference(double?r)?{

public?double?Area(double?r)?{

return?PI*r*r;

*?圆柱类,继承自圆类

public?class?Cylinder?extends?Circle{

private?double?h;

public?Cylinder(double?r,?double?h)?{

super(r);

this.h?=?h;

public?double?CeArea(double?r,?double?h)?{

return?super.Circumference(r)*h;

public?double?Volume(double?r,?double?h)?{

return?super.Area(r)*h;

*?圆锥类,继承自圆柱类

public?class?Cone?extends?Cylinder{

public?Cone(double?r,?double?h)?{

super(r,?h);

*?测试类

public?class?Test?{

public?static?void?main(String[]?args)?{

Circle?circle?=?new?Circle(r);

System.out.println("半径为:"?◆?r?◆?"?圆的周长为:"?◆?circle.Circumference(r));

System.out.println("半径为:"?◆?r?◆?"?圆的面积为:"?◆?circle.Area(r));

System.out.println("底部半径为:"?◆?r?◆?",高为:"?◆?h?◆?"?圆柱的侧面积为:"?◆?cylinder.CeArea(r,?h));

System.out.println("底部半径为:"?◆?r?◆?",高为:"?◆?h?◆?"?圆柱的体积为:"?◆?cylinder.Volume(r,?h));

System.out.println("底部半径为:"?◆?r?◆?",高为:"?◆?h?◆?"?圆锥的侧面积为:"?◆?cone.CeArea(r,?h));

System.out.println("底部半径为:"?◆?r?◆?",高为:"?◆?h?◆?"?圆锥的体积为:"?◆?cone.Volume(r,?h));

以上就是土嘎嘎小编为大家整理的继承代码java相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章