公司支出的总薪水
abstract class Employee{
public abstract double earnings();
}
class YearWorker extends Employee{
public double earnings(){
return 12000;
}
}
class MonthWorker extends Employee{
public double earnings(){
return 52*78;
}
}
class WeekWorker extends Employee{
public double earnings(){
return 12*2300;
}
}
class Company{
Employee[] employee;
double salaries=0;
Company(Employee[]employee){
this.employee=employee;
}
public double salariesPay(){
salaries=0;
for (int i=0;i<employee.length;i++){
salaries=salaries+employee[i].earnings();
}
return salaries;
}
}
public class CompanySalary {
public static void main(String args[]){
Employee[] employee=new Employee[29];
for(int i=0;i<employee.length;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("公司薪水总额:"+company.salariesPay()+"元");
}
}
银行计算总利息
public class BankOfDalian extends Bank {
double year;
public double computerInterest(){
super.year=(int)year;
double r=year-(int)year;
int day=(int)(r*1000);
double yearInterest=super.computerInterest();
double dayInterest=day*0.00012*savedMoney;
interest=yearInterest+dayInterest;
System.out.println("%d元存在大连银行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);
return interest;
}
}
public class Construction extends Bank{
double year;
public double computerInterest(){
super.year=(int)year;
double r=year-(int)year;
int day=(int)(r*1000);
double yearInterest=super.computerInterest();
double dayInterest=day*0.001*savedMoney;
interest=yearInterest+dayInterest;
System.out.println("%d元存在建设银行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);
return interest;
}
}
银行计算总利息
public class Bank {
int savedMoney;
int year;
double interest;
double interestRate=0.29;
public double computerInterest(){
interest=year*interestRate*savedMoney;
return interest;
}
public void setInterestRate(double rate){
interestRate=rate;
}
}
public class SaveMoney {
public static void main(String args[]){
int amount=8000;
Construction bank1=new Construction();
bank1.savedMoney=amount;
bank1.year=8.236;
bank1.setInterestRate(0.035);
double interest1=bank1.computerInterest();
BankOfDalian bank2=new BankOfDalian();
bank2.savedMoney=amount;
bank2.year=8.236;
bank2.setInterestRate(0.035);
double interest2=bank2.computerInterest();
System.out.printf("两个银行的利息相差%f元\n",interest2-interest1);
}
}
中国人,北京人,美国人
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();5
}
}
public class BeijingPeople extends ChinaPeople{
public void averageHieght(){
System.out.println("北京人的平均身高:"+height+"厘米");
}
public void averageWeight(){
System.out.println("北京人的平均体重:"+weight+"千克");
}
public void beijingOpera(){
System.out.println("花脸,青衣,花旦和老生");
}
}
public class ChinaPeople extends People{
public void speakHello(){
System.out.println("您好");
}
public void averageHeight(){
height=168.78;
System.out.println("中国人的平均身高:"+height+"厘米");
}
public void averageWeight(){
System.out.println("中国人的平均体重是65千克");
}
public void chinaGongfu(){
System.out.println("坐如钟,站如松,睡如弓");
}
}
public class AmericanPeople extends People{
public void speakHello(){
System.out.println("How do you do?");
}
public void averageHeight(){
System.out.println("American's average height:176 cm");
}
public void averageWeight(){
weight=75;
System.out.println("American's average weight:"+weight+"kg");
}
public void americanBoxing(){
System.out.println("直拳·勾拳·组合拳");}
}
public class People {
protected double weight;
protected double height;
public void speakHello(){
System.out.println("yayayaya");
}
public void averageHeight(){
height=173;
System.out.println("average height:"+height);
}
public void averageWeight(){
weight=70;
System.out.println("average weight:"+weight);
}
}
在这里插入代码片
private只有这个类的内部都可以访问,类内部指类的成员函数和定义初始化,这个限制是对类的而不是对对象的
对象是实体需要被创建,类是规范,根据类的定义来创建对象
对象的变量是对象的管理者
定义在函数内部的变量是本地变量
本地变量的生存期和作用域都是函数内部,成员变量的生存期是对象的生存期,作用域是类内部的成员函数