public class JIhe {
成都創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、察隅網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場景定制、電子商務(wù)商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為察隅等各大城市提供網(wǎng)站開發(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中,用一個類同時繼承一個類和實現(xiàn)一個接口代碼如下:
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ū)ο笞铒@著的一個特性。繼承是從已有的類中派生出新的類,新的類能吸收已有類的數(shù)據(jù)屬性和行為,并能擴展新的能力。Java繼承是使用已存在的類的定義作為基礎(chǔ)建立新類的技術(shù),新類的定義可以增加新的數(shù)據(jù)或新的功能,也可以用父類的功能,但不能選擇性地繼承父類。
接口實現(xiàn):接口實現(xiàn)在java中是一種特殊繼承方式,接口在定義后,就可以在類中實現(xiàn)該接口,在類中實現(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("我是一只鳥。");
}
}
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();
}
}
第一個:
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("蜘蛛在織網(wǎng)");
}
}
第三個類
public?class?Baigujing?extends?Yaojing?{
public?void?beBeauty(){
System.out.println("白骨精");
}
}
java基礎(chǔ),繼承類題目:編寫一個Java應(yīng)用程序,該程序包括3個類:Monkey類、People類和主類 E
21.編寫一個Java應(yīng)用程序,該程序包括3個類:Monkey類、People類和主類
E。要求:
(1) Monkey類中有個構(gòu)造方法:Monkey (String s),并且有個public void speak()
方法,在speak方法中輸出“咿咿呀呀......”的信息。
(2)People類是Monkey類的子類,在People類中重寫方法speak(),在speak方法
中輸出“小樣的,不錯嘛!會說話了!”的信息。
(3)在People類中新增方法void think(),在think方法中輸出“別說話!認(rèn)真思考!”
的信息。
(4)在主類E的main方法中創(chuàng)建Monkey與People類的對象類測試這2個類的功
能。、
復(fù)制代碼
package zhongqiuzuoye;
public class Monkey {
Monkey(String s) //構(gòu)造
{}
public void speak()
{
System.out.println("咿咿呀呀......");
}
}