可運行的:
創(chuàng)新互聯(lián)主營新鄉(xiāng)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),新鄉(xiāng)h5小程序開發(fā)搭建,新鄉(xiāng)網(wǎng)站營銷推廣歡迎新鄉(xiāng)等地區(qū)企業(yè)咨詢
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 JIhe {
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());
}
}
情況是這樣的:
Class Two 繼承了 Class One ,因此就可以使用 printAB() ,
當(dāng)執(zhí)行到 語句: t.printAB(), 時,會跳轉(zhuǎn)到 Class One 中,執(zhí)行 printAB(),
然后,發(fā)現(xiàn) 里面有個 printA(), 于是 去 Class Two中找,因為修飾符為 protected 能被Class One看到,所以 打印 TwoA;
然后,又發(fā)現(xiàn)有個 printB() , 于是去Class Two中找,因為修飾符是 private 所以,不能被Class One看到,所以 打印 OneB;
第一個:
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("白骨精");
}
}
代碼如下:
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 Inheritance3
{
public static void main(String[] args)
{
Son son = new Son(); // 這里只是實例化Son類所以只能調(diào)用Son中的方法
son.son() ;
Father father = new Son() ;//Son是Father的子類 可以通過子類實例化父類
father.father() ; //調(diào)用父類中的方法
Grandpa grandpa = new Son() ; //Son是Father的子類,F(xiàn)ather是Grandpa的子類,那么Son也是Grandpa的子類
grandpa.grandpa() ;
}
}
class Grandpa
{
public void grandpa()
{
System.out.println("grandpa");
}
}
class Father extends Grandpa
{
public void father()
{
System.out.println("father");
}
}
class Son extends Father
{
public void son()
{
System.out.println("son");
}
}