public class JIhe {
成都創新互聯是專業的豐滿網站建設公司,豐滿接單;提供網站設計、成都網站制作,網頁設計,網站設計,建網站,PHP網站建設等專業做網站服務;采用PHP框架,可快速的進行豐滿網站開發網頁制作和功能擴展;專業做搜索引擎喜愛的網站,專業的做網站團隊,希望更多企業前來合作!
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基礎,繼承類題目:編寫一個Java應用程序,該程序包括3個類:Monkey類、People類和主類 E
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("咿咿呀呀......");
}
}
可運行的:
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();
}
}
第一個:
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("蜘蛛在織網");
}
}
第三個類
public?class?Baigujing?extends?Yaojing?{
public?void?beBeauty(){
System.out.println("白骨精");
}
}
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;
/**
*?測試類
*?@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?+?"?圓的周長為:"?+?circle.Circumference(r));
System.out.println("半徑為:"?+?r?+?"?圓的面積為:"?+?circle.Area(r));
Cylinder?cylinder?=?new?Cylinder(3,?2);
System.out.println("底部半徑為:"?+?r?+?",高為:"?+?h?+?"?圓柱的側面積為:"?+?cylinder.CeArea(r,?h));
System.out.println("底部半徑為:"?+?r?+?",高為:"?+?h?+?"?圓柱的體積為:"?+?cylinder.Volume(r,?h));
Cone?cone?=?new?Cone(3,?2);
System.out.println("底部半徑為:"?+?r?+?",高為:"?+?h?+?"?圓錐的側面積為:"?+?cone.CeArea(r,?h));
System.out.println("底部半徑為:"?+?r?+?",高為:"?+?h?+?"?圓錐的體積為:"?+?cone.Volume(r,?h));
}
}
本文標題:java繼承的程序代碼,java繼承的實現
分享路徑:http://vcdvsql.cn/article28/hejojp.html
成都網站建設公司_創新互聯,為您提供服務器托管、移動網站建設、小程序開發、軟件開發、網站營銷、定制網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯