導航:首頁 > 編程語言 > java繼承的例子

java繼承的例子

發布時間:2023-06-04 17:31:04

1. java程序繼承

packageextend;

/**
*圓類
*@author楓雅
*2019年3月21日
*/
publicclassCircle{
privatedoubler;
publicfinalstaticdoublePI=3.14;
publicCircle(doubler){
this.r=r;
}

publicdoubleCircumference(doubler){
return2*PI*r;
}

publicdoubleArea(doubler){
returnPI*r*r;
}
}
packageextend;

/**
*圓柱類,繼承自圓類
*@author楓雅
*2019年3月21日
*/
{

privatedoubleh;
publicCylinder(doubler,doubleh){
super(r);
this.h=h;
}

publicdoubleCeArea(doubler,doubleh){
returnsuper.Circumference(r)*h;
}

publicdoubleVolume(doubler,doubleh){
returnsuper.Area(r)*h;
}
}
packageextend;

/**
*圓錐類,繼承自圓柱類
*@author楓雅
*2019年3月21日
*/
{

publicCone(doubler,doubleh){
super(r,h);
}

publicdoubleCeArea(doubler,doubleh){
returnsuper.CeArea(r,h)/2;
}

publicdoubleVolume(doubler,doubleh){
returnsuper.Volume(r,h)/3;
}
}
packageextend;

/**
*測試類
*@author楓雅
*2019年3月21日
*/
publicclassTest{

publicstaticvoidmain(String[]args){
doubler=3;
doubleh=2;
Circlecircle=newCircle(r);
System.out.println("半徑為:"+r+"圓的周長為:"+circle.Circumference(r));
System.out.println("半徑為:"+r+"圓的面積為:"+circle.Area(r));

Cylindercylinder=newCylinder(3,2);
System.out.println("底部半徑為:"+r+",高為:"+h+"圓柱的側面積為:"+cylinder.CeArea(r,h));
System.out.println("底部半徑為:"+r+",高為:"+h+"圓柱的體積為:"+cylinder.Volume(r,h));

Conecone=newCone(3,2);
System.out.println("底部半徑為:"+r+",高為:"+h+"圓錐的側面積為:"+cone.CeArea(r,h));
System.out.println("底部半徑為:"+r+",高為:"+h+"圓錐的體積為:"+cone.Volume(r,h));
}

}

2. 如何在Java中使用子類繼承父類的父類,舉個例子看看,謝謝

class Animal{//動物類
public String name="haha";//名字
public void say() {//說話方法
System.out.println(name+" say hi");
}
}
class Cat extends Animal{//貓類繼承了動物類

}
public class Test {
public static void main(String[] args) {
Cat cat=new Cat();//實例化一個貓
cat.say();//由於貓繼承了動物,所以繼承了他所有非private的屬性和方法
}
}

3. 求java中幾個繼承與多態實例,並要有相應的題目!給我參考和練習下,本人新手,不宜難的

JAVA中一個抽象類抽象方法繼承與對象多態性的例子
面向對象的三大特點:封裝,繼承,多態。

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

一個繼承與對象多態性的例子:聲明一個Person 類。Student 類,Worker類分別繼承Person。

人有姓別,年齡,學生有特有的成績屬性,工人有特有的工資。

所有屬性都用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 "學生信息------>姓名:"+super.getName()+", 年齡:"+super.getAge()+",成績:"+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();

}
}

運行結果:

學生信息------>姓名:張三, 年齡:23,成績:90.0
工人信息------>姓名:李師傅,年齡:26,工資:3000.0

4. 簡單的java 編程題 關於繼承

package javaapplication4;
public class Rect {
protected int length;/////這個地方不能變成私有屬性,因為後面繼承的類也需要繼承它。
protected int width;
public Rect(int length,int width)
{
this.length=length;
this.width=width;
}
public void setLength(int length)
{this.length=length;<br> }
public void setWidth(int width)
{this.width=width;<br> }
public int getLength()
{return length;<br> }
public int getWidth()
{return width;<br> }
public int getArea()
{return length*width;<br> }
public int getCycle()
{return (length+width)*2;<br> }}
////////////////////////////////////////////////////////////////////////////////////////////////////////
package javaapplication4;
public class PlainRect extends Rect
{///此類並未繼承父類的長寬屬性,所以父類的設計是不合理的。應將其屬性改為protected.
protected int startX,startY;
public PlainRect(int length,int width,int startx,int starty)
{
super(length,width);
startX=startx;
startY=starty;
}
public PlainRect()
{
super(0,0);
startX=0;
startY=0;
}
public boolean isInside(double x,double y)
{
boolean b=false;
if(x>startX&&x<startX+length)
if(y>startY&&y<startY+width)
b=true;
return b; }}
////////////////////////////////////////////////////////////////////////////////////////////////////////
package javaapplication4;
public class Main {
public static void main(String[] args) {
PlainRect Pr1=new PlainRect(20,10,10,10);
System.out.println("面積為:"+Pr1.getArea());
System.out.println("周長為:"+Pr1.getCycle());
System.out.println("點(25.5,13)"+(Pr1.isInside(25.5, 13)?"在":"不在")+"此方形內");
} }

閱讀全文

與java繼承的例子相關的資料

熱點內容
ipad解壓專家怎麼解壓qq郵箱文件 瀏覽:251
php712安裝 瀏覽:448
python遠程桌面控制 瀏覽:215
操作系統scan演算法 瀏覽:11
伺服器板塊有什麼龍頭 瀏覽:74
我的世界伺服器成員怎麼開創造 瀏覽:660
程序員鄭州買房哪個區好 瀏覽:203
程序員發怒 瀏覽:823
安卓機看視頻怎麼沒有小窗口 瀏覽:456
minecraft伺服器怎麼布置 瀏覽:306
怎麼把安卓的東西轉到已激活蘋果 瀏覽:852
停止服務doss命令 瀏覽:877
u盤占內存但該文件夾為空 瀏覽:612
伺服器怎麼更換重生點 瀏覽:34
收費api調用平台源碼 瀏覽:647
安卓怎麼自檢病毒 瀏覽:560
布卡雲伺服器 瀏覽:770
程序員是怎麼做系統的 瀏覽:745
燕窩溯源碼最大加工廠 瀏覽:939
黑馬程序員第28集 瀏覽:487