public class JIhe {

成都創(chuàng)新互聯(lián)公司專注于企業(yè)營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、察隅網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制、電子商務(wù)商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為察隅等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
private String color;
private String dateCreated;
private String filled;
public String getColor() {
return color;
}
public String getDateCreated() {
return dateCreated;
}
public String getFilled() {
return filled;
}
public void setColor(String color) {
this.color = color;
}
public void setFilled(String filled) {
this.filled = filled;
}
@Override
public String toString() {
return "Color:" + this.color +" filled:" + this.filled + "detaCreated:" + dateCreated;
}
}
------------------------------------------------------------------------------------------------------------
public class Circle extends JIhe {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return 3.14 * this.radius * this.radius;
}
public double getPerimeter() {
return 2 * 3.14 * this.radius;
}
public double getDiameter() {
return 2 * this.radius;
}
}
-----------------------------------------------------------------------------------------------------
public class Rectangle extends JIhe {
private double width;
private double height;
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea(){
return this.width * this.height;
}
public double getPerimeter(){
return this.width * 2 + this.height * 2;
}
}
——————————————————————————————————————
public class Test {
public static void main(String[] args){
Circle circle = new Circle();
circle.setRadius(1.0);
System.out.println(circle.getArea());
System.out.println(circle.getColor());
System.out.println(circle.getDateCreated());
System.out.println(circle.getDiameter());
System.out.println(circle.getFilled());
System.out.println(circle.getPerimeter());
System.out.println(circle.getRadius());
Rectangle r = new Rectangle();
r.setHeight(2.0);
r.setWidth(4.0);
System.out.println(r.getArea());
System.out.println(r.getColor());
System.out.println(r.getDateCreated());
System.out.println(r.getFilled());
System.out.println(r.getHeight());
System.out.println(r.getPerimeter());
System.out.println(r.getWidth());
}
}
在 java中,用一個(gè)類同時(shí)繼承一個(gè)類和實(shí)現(xiàn)一個(gè)接口代碼如下:
class?Pigeon??extends?Bird?implements??Flyanimal?
{ ?? public?void?fly()
{
System.out.println("pigeon??can?fly");
}
public?void?egg()
{
System.out.println("pigeon??can?lay??eggs?");
}
}
類繼承:繼承是面向?qū)ο笞铒@著的一個(gè)特性。繼承是從已有的類中派生出新的類,新的類能吸收已有類的數(shù)據(jù)屬性和行為,并能擴(kuò)展新的能力。Java繼承是使用已存在的類的定義作為基礎(chǔ)建立新類的技術(shù),新類的定義可以增加新的數(shù)據(jù)或新的功能,也可以用父類的功能,但不能選擇性地繼承父類。
接口實(shí)現(xiàn):接口實(shí)現(xiàn)在java中是一種特殊繼承方式,接口在定義后,就可以在類中實(shí)現(xiàn)該接口,在類中實(shí)現(xiàn)接口可以使用關(guān)鍵字implement。
創(chuàng)建父類:
class?Bird?{
int?legnum?=?2;?????void?egg()?{????};}
定義接口:
interface?Flyanimal
{ ?
void?fly();
}
代碼如下:
abstract?class?DongWu?{
public?abstract?void?info();
}
class?Bird?extends?DongWu?{
@Override
public?void?info()?{
System.out.println("我是一只鳥(niǎo)。");
}
}
class?Fish?extends?DongWu?{
@Override
public?void?info()?{
System.out.println("我是一條魚(yú)。");
}
}
public?class?App5?{
public?static?void?main(String[]?args)?{
DongWu?bird?=?new?Bird();
bird.info();
DongWu?fish?=?new?Fish();
fish.info();
}
}
第一個(gè):
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?+?"]";
}
}
第二個(gè)類
public?class?Zhizhujing?extends?Yaojing?{
public?void?buildNet(){
System.out.println("蜘蛛在織網(wǎng)");
}
}
第三個(gè)類
public?class?Baigujing?extends?Yaojing?{
public?void?beBeauty(){
System.out.println("白骨精");
}
}
java基礎(chǔ),繼承類題目:編寫(xiě)一個(gè)Java應(yīng)用程序,該程序包括3個(gè)類:Monkey類、People類和主類 E
21.編寫(xiě)一個(gè)Java應(yīng)用程序,該程序包括3個(gè)類:Monkey類、People類和主類
E。要求:
(1) Monkey類中有個(gè)構(gòu)造方法:Monkey (String s),并且有個(gè)public void speak()
方法,在speak方法中輸出“咿咿呀呀......”的信息。
(2)People類是Monkey類的子類,在People類中重寫(xiě)方法speak(),在speak方法
中輸出“小樣的,不錯(cuò)嘛!會(huì)說(shuō)話了!”的信息。
(3)在People類中新增方法void think(),在think方法中輸出“別說(shuō)話!認(rèn)真思考!”
的信息。
(4)在主類E的main方法中創(chuàng)建Monkey與People類的對(duì)象類測(cè)試這2個(gè)類的功
能。、
復(fù)制代碼
package zhongqiuzuoye;
public class Monkey {
Monkey(String s) //構(gòu)造
{}
public void speak()
{
System.out.println("咿咿呀呀......");
}
}
分享文章:java繼承類的編程代碼 Java繼承代碼
瀏覽地址:http://chinadenli.net/article18/dodpddp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、服務(wù)器托管、電子商務(wù)、定制網(wǎng)站、動(dòng)態(tài)網(wǎng)站、虛擬主機(jī)
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)