java繼承的代碼例子 java類的繼承代碼

Java關(guān)于繼承的問題,代碼如下。

變量相同字父類不會(huì)被覆蓋,而方法相同子類會(huì)覆蓋父類方法,java在調(diào)用方法時(shí)會(huì)調(diào)用實(shí)際new時(shí)對(duì)象的方法,new Student 那么如果Student中有fun則調(diào)用,沒有才查找父類中有沒有fun方法,而屬性會(huì)直接根據(jù)引用調(diào)用,引用是Person,就調(diào)用Person的i,寫程序時(shí)是根據(jù)引用來寫的,所以不可能你引用Person,會(huì)寫出子類的屬性,比如Student有個(gè)自己的屬性j,你通過to肯定找不到j(luò),如果引用是Student則調(diào)用Student的i,

嘉陵ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!

求java中幾個(gè)繼承與多態(tài)實(shí)例,并要有相應(yīng)的題目!給我參考和練習(xí)下,本人新手,不宜難的

JAVA中一個(gè)抽象類抽象方法繼承與對(duì)象多態(tài)性的例子

面向?qū)ο蟮娜筇攸c(diǎn):封裝,繼承,多態(tài)。

在JAVA中我們總是盡可能地讓一個(gè)類繼承一個(gè)抽象類,這樣大大的節(jié)省代碼方便開發(fā)。

一個(gè)繼承與對(duì)象多態(tài)性的例子:聲明一個(gè)Person 類。Student 類,Worker類分別繼承Person。

人有姓別,年齡,學(xué)生有特有的成績(jī)屬性,工人有特有的工資。

所有屬性都用private封裝

abstract class Person{

private String name;

private int age;

public Person(String name,int age){

this.setName(name);

this.setAge(age);

}

public void setName(String name){

this.name=name;

}

public void setAge(int age){

this.age=age;

}

public String getName(){

return this.name;

}

public int getAge(){

return this.age;

}

public void say(){

System.out.println(this.getContent());

}

public abstract String getContent();

}

class Worker extends Person{

private float salary;

public Worker(String name,int age,float salary){

super(name,age);

this.setSalary(salary);

}

public void setSalary(float salary){

this.salary=salary;

}

public float getSalary(){

return this.salary;

}

public String getContent(){

return "工人信息------姓名:"+super.getName()+",年齡:"+super.getAge()+",工資:"+this.getSalary();

}

}

class Student extends Person{

private float score;

public Student(String name,int age,float score){

super(name,age);

this.setScore(score);

}

public void setScore(float score){

this.score=score;

}

public float getScore(){

return this.score;

}

public String getContent(){

return "學(xué)生信息------姓名:"+super.getName()+", 年齡:"+super.getAge()+",成績(jī):"+this.getScore();

}

}

public class OODemo11{

public static void main(String []args){

Person p=null;

p=new Student("張三",23,90);

p.say();

p=new Worker("李師傅",26,3000);

p.say();

}

}

運(yùn)行結(jié)果:

學(xué)生信息------姓名:張三, 年齡:23,成績(jī):90.0

工人信息------姓名:李師傅,年齡:26,工資:3000.0

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;

/**

*?測(cè)試類

*?@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?+?"?圓的周長(zhǎng)為:"?+?circle.Circumference(r));

System.out.println("半徑為:"?+?r?+?"?圓的面積為:"?+?circle.Area(r));

Cylinder?cylinder?=?new?Cylinder(3,?2);

System.out.println("底部半徑為:"?+?r?+?",高為:"?+?h?+?"?圓柱的側(cè)面積為:"?+?cylinder.CeArea(r,?h));

System.out.println("底部半徑為:"?+?r?+?",高為:"?+?h?+?"?圓柱的體積為:"?+?cylinder.Volume(r,?h));

Cone?cone?=?new?Cone(3,?2);

System.out.println("底部半徑為:"?+?r?+?",高為:"?+?h?+?"?圓錐的側(cè)面積為:"?+?cone.CeArea(r,?h));

System.out.println("底部半徑為:"?+?r?+?",高為:"?+?h?+?"?圓錐的體積為:"?+?cone.Volume(r,?h));

}

}

JAVA 繼承的代碼 求幫助 在線等

簡(jiǎn)單寫了下,希望對(duì)你有幫助

建議你找一下Java轉(zhuǎn)型方面的資料

class Vehicle{

protected void vehicleRun(){

System.out.println("vehicle!!!");

}

}

class Truck extends Vehicle{

//子類覆寫父類“vehicleRun方法”

protected void vehicleRun(){

System.out.println("It's Truck!!!");

}

protected void truckRun(){

System.out.println("truck!!!");

}

}

class Test{

public static void main(String [] args){

Vehicle v = new Vehicle();

v.vehicleRun(); //父類方法

//向上轉(zhuǎn)型

Vehicle v1 = new Truck();

v1.vehicleRun(); //子類覆寫后方法

//向下轉(zhuǎn)型

Truck t = (Truck)v1;

t.vehicleRun();

t.truckRun();

}

}

Java編程:舉例說明:繼承,并輸出結(jié)果。

public class Test {

public static void main(String[] args){

Test2 t2 = new Test2();

t2.t1();

System.out.println("繼承的變量:"+t2.i);

}

}

class Test1{

int i=100;

public void t1(){

System.out.println("這是父類的方法");

}

}

class Test2 extends Test1{

}

簡(jiǎn)述JAVA中繼承實(shí)現(xiàn)代碼復(fù)用

看看下面這個(gè)例子,就會(huì)明白了:JAVA中繼承可以實(shí)現(xiàn)代碼復(fù)用,

由于在父類中已經(jīng)定義的方法,被子類繼承以后,就可以使用,實(shí)現(xiàn)了代碼的復(fù)用

class Father{

private int moneyDollar=300;

int moneyHK=200;

int add(int x,int y){

return x+y;

}

}

class Son extends Father{

int moneyRMB=800;

public void changMoneyHK(int x){

moneyHK=x;

}

public void changMoneyRMB(int x){

moneyRMB=x;

}

int subs(int x,int y){

return x-y;

}

}

class GrandSon extends Son{

int multi(int x,int y){

return x*y;

}

}

public class Example5_1{

public static void main(String args[]){

int a=5,b=3;

Son son=new Son();

GrandSon sunzi=new GrandSon();

son.changMoneyHK(666);

son.changMoneyRMB(5000);

System.out.println("兒子的港幣是繼承的屬性,當(dāng)前的值是:"+son.moneyHK);

System.out.println("兒子的人民幣是新增的屬性,當(dāng)前的值是:"+son.moneyRMB);

System.out.printf("減法是兒子新增的功能,%d-%d等于%d\n",a,b,son.subs(a,b));

System.out.printf("加法是兒子繼承的功能,%d+%d等于%d\n",a,b,son.add(a,b));

System.out.println("孫子的港幣和人民幣都是繼承的屬性,,當(dāng)前的值是:");

System.out.println("港幣:"+sunzi.moneyHK+" 人民幣:"+sunzi.moneyRMB);

System.out.printf("乘法是孫子新增的功能,%d*%d等于%d\n",a,b,sunzi.multi(a,b));

System.out.printf("加法是孫子繼承的功能,%d+%d等于%d\n",a,b,sunzi.add(a,b));

System.out.printf("減法是孫子繼承的功能,%d-%d等于%d\n",a,b,sunzi.subs(a,b));

}

}

本文名稱:java繼承的代碼例子 java類的繼承代碼
文章URL:http://muchs.cn/article4/docdgie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、外貿(mào)建站、全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站