Pages: 1/89 First page 1 2 3 4 5 6 7 8 9 10 Next page Final page [ View by Articles | List ]

实验九 类继承与接口(二)

| May 1, 2009 10:10 | timmy | Via Original
1.上转型对象(实验教材5.2.2)
HardWork.java

abstract class Employee{
  public abstract double earnings();
}
class YearWorker extends Employee{
  public double earnings(){
    return 200000;    
  }
}
class MonthWorker extends Employee{
  public double earnings(){
    return 10000;    
  }
}
class WeekWorker extends Employee{
  public double earnings(){
    return 5000;    
  }
}
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+=employee[i].earnings();
    }
    return salaries;
  }
}

public class HardWork {
  public static void main(String[] args) {
    Employee[] employee=new Employee[20];
    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());
  }
}
Tags:

实验八 类继承与接口(一)

| April 10, 2009 15:36 | timmy | Via Original
1.继承(实验教材5.2.1)
Example.java

class People{
  protected double weight,height;
  public void speakHello(){
    System.out.println("yayawawa");
  }
  public void averageHeight(){
    height=173;
    System.out.println("Average Height:"+height);
  }
  public void averageWeight(){
    weight=70;
    System.out.println("Average Weight:"+weight);
  }
}
class ChinaPeople extends People{
  public void speakHello(){
    System.out.println("你好,吃了吗");
  }  
  public void averageHeight(){
    height=173;
    System.out.println("中国人的平均身高:"+height+"厘米");
  }
  public void averageWeight(){
    weight=67.34;
    System.out.println("中国人的平均体重:"+weight+"公斤");
  }
  public void chinaGongfu(){
    System.out.println("坐如钟,站如松,睡如弓");
  }
}
class AmericanPeople extends People{
  public void speakHello(){
    System.out.println("How do you do");
  }
  public void averageHeight(){
    height=188;
    System.out.println("American Average Height:"+height+"cm");
  }
  public void averageWeight(){
    weight=80.23;
    System.out.println("American Average Weight:"+weight+"kg");
  }
  public void americanBoxing(){
    System.out.println("直拳,勾拳");
  }
}
class BeijingPeople extends ChinaPeople{
  public void speakHello(){
    System.out.println("您好");
  }  
  public void averageHeight(){
    height=16;
    System.out.println("北京人的平均身高:"+height+"厘米");
  }
  public void averageWeight(){
    weight=6;
    System.out.println("北京人的平均体重:"+weight+"公斤");
  }
  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();
  }
  
}
Tags:

实验七 类与对象(四)

| April 10, 2009 15:34 | timmy | Via Original
1.强化实验四
2.Class类的使用(实验教材4.3)
ExampleOne.java

import java.lang.reflect.*;
class A{
  int x;
  float y;
  double z;
  A(){
    x=12;
    y=12.901f;
    z=0.123456;
  }
  A(int x,float y,double z){
    this.x=x;
    this.y=y;
    this.z=z;
  }
  public double getSum(){
    return x+y+z;
  }
  public void setX(int x){
    this.x=x;
  }
  public void setY(float y){
    this.y=y;
  }
  public void setZ(double z){
    this.z=z;
  }
}

public class ExampleOne {

  public static void main(String[] args) {
    A a=new A(12,34.9f,0.54321);
    Class cs=a.getClass();
    String className=cs.getName();
    Constructor[] con=cs.getDeclaredConstructors();
    Field[] field=cs.getDeclaredFields();
    Method[] method=cs.getDeclaredMethods();
    System.out.println("类的名字:"+className);
    System.out.println("类中有如下的成员变量:");
    for(int i=0;i<field.length;i++)
      System.out.println(field[i].toString());
    System.out.println("类中有如下的方法:");
    for(int i=0;i<method.length;i++)
      System.out.println(method[i].toString());
    System.out.println("类中有如下的构造方法:");
    for(int i=0;i<con.length;i++)
      System.out.println(con[i].toString());
    
  }

}
Tags:

实验六 类与对象(三)

| April 10, 2009 15:32 | timmy | Via Original
1.package语句与import语句(实验教材4.2.3)
SunRise.java

import tom.jiafei.Trangle;
import java.util.Date;
class SunRise {
  public static void main(String[] args) {
    Trangle trangle=new Trangle(12,3,104);
    trangle.computeArea();
    trangle.modifyThreeSide(3,4,5);
    trangle.computeArea();
    Date date=new Date();
    System.out.println(date);    
  }

}
Tags:

实验五 类与对象(二)

| April 10, 2009 15:29 | timmy | Via Original
1.实例成员与类成员(实验教材4.2.2)
Ex4_2.java

class A {
  float a;
  static float b;
  void setA(float a){
    this.a=a;
  }
  void setB(float b){
    A.b=b;
  }
  float getA(){
    return a;
  }
  float getB(){
    return b;
  }
  void inputA(){
    System.out.println(a);
  }
  static void inputB(){
    System.out.println(b);
  }
}
public class Ex4_2 {
  public static void main(String[] args) {
    A.b=100;
    A.inputB();
    A cat=new A();
    A dog=new A();
    cat.setA(200);
    cat.setB(400);
    dog.setA(200);
    dog.setB(900);
    cat.inputA();
    cat.inputB();
    dog.inputA();
    dog.inputB();
  }

}
Tags:
Pages: 1/89 First page 1 2 3 4 5 6 7 8 9 10 Next page Final page [ View by Articles | List ]