}
继承
模板代码
Example.java
class People {
protected double weight,height; public void speakHello() {
System.out.println(\ }
public void averageHeight() {
height=173;
System.out.println(\ }
public void averageWeight() {
weight=70;
System.out.println(\ } }
class ChinaPeople extends People {
public void speakHello() { System.out.println(\你好,吃饭了吗?\
} //重写public void speakHello()方法,要求输出类似“你好,吃了吗”这样的
//汉语信息
public void averageHeight() { height=173; System.out.println(\中国人的平均身高:\厘米\ } //重写public void averageHeight()方法,要求输出类似
//“中国人的平均身高:168.78厘米”这样的汉语信息
public void averageWeight() {
weight=67.34; System.out.println(\中国人的平均体重:\公斤\
} //重写public void averageWeight()方法,
//要求输出类似“中国人的平均体重:65公斤”这样的汉语信息 public void chinaGongfu() {
System.out.println(\坐如钟,站如松,睡如弓\//输出中国武术的信息,例如:\坐如钟,站如松,睡如弓\等 } }
class AmericanPeople extends People {
public void speakHello() { System.out.println(\ou do\ } //重写public void speakHello()方法,要求输出类似
//“How do you do”这样的英语信息。
public void averageHeight()
{ height=188; System.out.println(\ } //重写public void averageHeight()方法
public void averageWeight()
{ weight=80.23; System.out.println(\ } //重写public void averageWeight()方法
public void americanBoxing() {
System.out.println(\直拳、钩拳\//输出拳击的信息,例如,“直拳”、“钩拳”等 } }
class BeijingPeople extends ChinaPeople {
public void speakHello() {
System.out.println(\您好\ } //重写public void speakHello()方法,要求输出类似“您好”这样的汉语信息
public void averageHeight()
{ height=16; System.out.println(\北京人的平均身高:\厘米\ } //重写public void averageHeight()方法
public void averageWeight()
{ weight=6;
System.out.println(\北京人的平均体重:\公斤\ } //重写public void averageWeight()方法
public void beijingOpera() {
System.out.println(\京剧术语\//输出京剧的信息 } }
public class Example {
public static void main(String args[]) {
ChinaPeople chinaPeople=new ChinaPeople();
AmericanPeople americanPeople=new AmericanPeople(); BeijingPeople beijingPeople=new BeijingPeople(); chinaPeople.speakHello(); americanPeople.speakHello(); beijingPeople.speakHello(); chinaPeople.averageHeight(); americanPeople.averageHeight(); beijingPeople.averageHeight(); chinaPeople.averageWeight(); americanPeople.averageWeight(); beijingPeople.averageWeight(); chinaPeople.chinaGongfu(); americanPeople.americanBoxing(); beijingPeople.beijingOpera() ; beijingPeople.chinaGongfu(); } }
上转型对象
模板代码 HardWork.java
abstract class Employee {
public abstract double earnings(); }
class YearWorker extends Employee {
public double earnings() { return 50000.456;
} //重写earnings()方法 }
class MonthWorker extends Employee {
public double earnings() { return 12*2300; } //重写earnings()方法。 }
class WeekWorker extends Employee {
public double earnings() { return 52*500; }//重写earnings()方法。 }
class Company {
Employee[] employee; double salaries=0;
Company(Employee[] employee) {
this.employee=employee; }
public double salariesPay() {
salaries=0;
for(int i=0;i public class HardWork { public static void main(String args[]) { Employee[] employee=new Employee[20]; for(int i=0;i if(i%3==0) employee[i]=new WeekWorker(); else if(i%3==1) employee[i]=new MonthWorker(); else if(i%3==2) employee[i]=new YearWorker(); } Company company=new Company(employee); System.out.println(\公司年工资总额:\ } } 接口回调 模板代码 Road.java interface ComputerWeight { public double computeWeight(); } class Television implements ComputerWeight { public double computeWeight() { return 45.5; } //实现computeWeight()方法。 } class Computer implements ComputerWeight { public double computeWeight() { return 65.5; } //实现computeWeight()方法。 } class WashMachine implements ComputerWeight { public double computeWeight() { return 145; } //实现computeWeight()方法。 } class Car { ComputerWeight[] goods; double totalWeights=0; Car(ComputerWeight[] goods) { this.goods=goods; } public double getTotalWeights() {