继承代码java,如何使用继承码

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

可运行的:

创新互联是一家专业提供晋安企业网站建设,专注与成都网站设计、网站建设、外贸网站建设HTML5、小程序制作等业务。10年已为晋安众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

import java.awt.*;

import java.awt.event.*;

public class BackJFrame extends Frame{

public BackJFrame(){

super("台球");

setSize(300,300);

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();

}

public static void main(String args[]){

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){

this.name=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(){

return name;

}

}class Wolf extends Animal {

public Wolf(String name){

super(name);

}

public Wolf(){}

public void eat(){

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

}

public void walk(){

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

}

public String toString(){

return name;

}

}class Goat extends Animal {

public Goat(String name){

super(name);

}

public Goat(){}

public void eat(){

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

}

public void walk(){

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

}

public String toString(){

return name;

}

}

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;

/**

* 圆类

* @author 枫雅

* 2019年3月21日

*/

public class Circle {

private double r;

public final static double PI = 3.14;

public Circle(double r) {

this.r = r;

}

public double Circumference(double r) {

return 2*PI*r;

}

public double Area(double r) {

return PI*r*r;

}

}

package extend;

/**

* 圆柱类,继承自圆类

* @author 枫雅

* 2019年3月21日

*/

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;

}

}

package extend;

/**

* 圆锥类,继承自圆柱类

* @author 枫雅

* 2019年3月21日

*/

public class Cone extends Cylinder{

public Cone(double r, double h) {

super(r, h);

}

public double CeArea(double r, double h) {

return super.CeArea(r, h)/2;

}

public double Volume(double r, double h) {

return super.Volume(r, h)/3;

}

}

package extend;

/**

* 测试类

* @author 枫雅

* 2019年3月21日

*/

public class Test {

public static void main(String[] args) {

double r = 3;

double h = 2;

Circle circle = new Circle(r);

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

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

Cylinder cylinder = new Cylinder(3, 2);

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

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

Cone cone = new Cone(3, 2);

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

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

}

}


当前标题:继承代码java,如何使用继承码
分享路径:http://pwwzsj.com/article/hsssso.html