资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java中写出继承代码 java继承的代码

求编写一个Java类的继承程序?

java基础,继承类题目:编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E

创新互联是一家以重庆网站建设公司、网页设计、品牌设计、软件运维、营销推广、小程序App开发等移动开发为一体互联网公司。已累计为混凝土搅拌罐等众行业中小客户提供优质的互联网建站和软件开发服务。

21.编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类

E。要求:

(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()

方法,在speak方法中输出“咿咿呀呀......”的信息。

(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法

中输出“小样的,不错嘛!会说话了!”的信息。

(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”

的信息。

(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功

能。、

复制代码

package zhongqiuzuoye;

public class Monkey {

Monkey(String s) //构造

{}

public void speak()

{

System.out.println("咿咿呀呀......");

}

}

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继承代码??谢谢

可运行的:

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实现继承

js继承有5种实现方式:

1、继承第一种方式:对象冒充

function Parent(username){

this.username = username;

this.hello = function(){

alert(this.username);

}

}

function Child(username,password){

//通过以下3行实现将Parent的属性和方法追加到Child中,从而实现继承

//第一步:this.method是作为一个临时的属性,并且指向Parent所指向的对象,

//第二步:执行this.method方法,即执行Parent所指向的对象函数

//第三步:销毁this.method属性,即此时Child就已经拥有了Parent的所有属性和方法

this.method = Parent;

this.method(username);//最关键的一行

delete this.method;

this.password = password;

this.world = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

var child = new Child("lisi","123456");

parent.hello();

child.hello();

child.world();

2、继承第二种方式:call()方法方式

call方法是Function类中的方法

call方法的第一个参数的值赋值给类(即方法)中出现的this

call方法的第二个参数开始依次赋值给类(即方法)所接受的参数

function test(str){

alert(this.name + " " + str);

}

var object = new Object();

object.name = "zhangsan";

test.call(object,"langsin");//此时,第一个参数值object传递给了test类(即方法)中出现的this,而第二个参数"langsin"则赋值给了test类(即方法)的str

function Parent(username){

this.username = username;

this.hello = function(){

alert(this.username);

}

}

function Child(username,password){

Parent.call(this,username);

this.password = password;

this.world = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

var child = new Child("lisi","123456");

parent.hello();

child.hello();

child.world();

3、继承的第三种方式:apply()方法方式

apply方法接受2个参数,

A、第一个参数与call方法的第一个参数一样,即赋值给类(即方法)中出现的this

B、第二个参数为数组类型,这个数组中的每个元素依次赋值给类(即方法)所接受的参数

function Parent(username){

this.username = username;

this.hello = function(){

alert(this.username);

}

}

function Child(username,password){

Parent.apply(this,new Array(username));

this.password = password;

this.world = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

var child = new Child("lisi","123456");

parent.hello();

child.hello();

child.world();

4、继承的第四种方式:原型链方式,即子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承

function Person(){

}

Person.prototype.hello = "hello";

Person.prototype.sayHello = function(){

alert(this.hello);

}

function Child(){

}

Child.prototype = new Person();//这行的作用是:将Parent中将所有通过prototype追加的属性和方法都追加到Child,从而实现了继承

Child.prototype.world = "world";

Child.prototype.sayWorld = function(){

alert(this.world);

}

var c = new Child();

c.sayHello();

c.sayWorld();

5、继承的第五种方式:混合方式

混合了call方式、原型链方式

function Parent(hello){

this.hello = hello;

}

Parent.prototype.sayHello = function(){

alert(this.hello);

}

function Child(hello,world){

Parent.call(this,hello);//将父类的属性继承过来

this.world = world;//新增一些属性

}

Child.prototype = new Parent();//将父类的方法继承过来

Child.prototype.sayWorld = function(){//新增一些方法

alert(this.world);

}

var c = new Child("zhangsan","lisi");

c.sayHello();

c.sayWorld();

用JAVA编写继承与接口联合的代码是什么?

interface CanSwim {

void swim();

}

interface CanFight {

void fight();

}

interface CanFly {

void fly();

}

class ActionCharacter {

void fight() {

System.out.println("this is ActionCharacter");

}

}

class Hero extends ActionCharacter implements CanSwim, CanFly {

@Override

public void swim() {

}

@Override

public void fly() {

}

}

public class Adventure {

public static void t(CanFight x) {

x.fight();

}

public static void u(CanSwim x) {

x.swim();

}

public static void v(CanFly x) {

x.fly();

}

public static void w(ActionCharacter x) {

x.fight();

}

public static void main(String[] args) {

Hero h = new Hero();

u(h);

v(h);

w(h);

}

}

用Java继承和多态实现编写代码

代码如下:

abstract class DongWu {

public abstract void info();

}

class Bird extends DongWu {

@Override

public void info() {

System.out.println("我是一只鸟。");

}

}

class Fish extends DongWu {

@Override

public void info() {

System.out.println("我是一条鱼。");

}

}

public class App5 {

public static void main(String[] args) {

DongWu bird = new Bird();

bird.info();

DongWu fish = new Fish();

fish.info();

}

}


网站名称:java中写出继承代码 java继承的代码
转载源于:http://cdkjz.cn/article/hiessp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220