类与对象

来源:互联网 发布:阿里云oss是干什么的 编辑:程序博客网 时间:2024/06/16 04:18

1.请定义一个交通工具(Vehicle)的类,其中有:

属性:速度(speed),体积(size)等

方法:移动(move()),设置速度(setSpeed(intspeed)),设置体积(setSize(int size))加速speedUp(),减速speedDown()等

在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变。

  1. public class Vehicle {  
  2.     int speed;  
  3.     int size;  
  4.       
  5.     public Vehicle(int speed, int size) {  
  6.         // TODO Auto-generated constructor stub  
  7.         this.speed = speed;  
  8.         this.size = size;  
  9.     }  
  10.   
  11.     public int getSpeed() {  
  12.         return speed;  
  13.     }  
  14.   
  15.     public void setSpeed(int speed) {  
  16.         this.speed = speed;  
  17.     }  
  18.   
  19.     public int getSize() {  
  20.         return size;  
  21.     }  
  22.   
  23.     public void setSize(int size) {  
  24.         this.size = size;  
  25.     }  
  26.   
  27.     void move(){  
  28.           
  29.     }  
  30.   
  31.     int speedUp(){  
  32.         return speed+10;  
  33.     }  
  34.      int speedDown(){  
  35.             return speed-5;  
  36.     }  
  37. }  

  1. public class VehicleTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.         // TODO Auto-generated method stub  
  5.         Vehicle  v = new Vehicle(10,5);   
  6.           
  7.         System.out.println("速度:"+v.getSpeed());  
  8.         System.out.println("体积:"+v.getSize());  
  9.         System.out.println("加速:"+v.speedUp());  
  10.         System.out.println("减速:"+v.speedDown());  
  11.           
  12.     }  
  13.   
  14. }  
2.打印当前时间。学习使用Date类和Calendar类

  1. import java.util.Calendar;  
  2. import java.util.Date;  
  3.   
  4. public class Time {  
  5.   
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         Date time=new Date();  
  9.         System.out.println(time);//获取时间  
  10.         Calendar cal =Calendar.getInstance();  
  11.          cal.setTime(new Date());  
  12.          int year=cal.get(Calendar.YEAR);  
  13.          int month=cal.get(Calendar.MONTH)+1;  //一月对应0;  
  14.          int day=cal.get(Calendar.DAY_OF_MONTH);  
  15.          System.out.print(" "+year+"年"+" "+month+"月"+day+"日");  
  16.     }  
  17.   
  18. }  
3.以Point类为基础,定义一个平面中的Circle类:

1、  编写一个无参的构造函数;

2、  编写一个有参的构造函数;

3、  在主函数中调用无参的构造函数生成圆的实例c1,调用有参的构造函数生成圆的实例c2,调用实例方法判断c1和c2是否相重叠。

  1. import java.util.*;  
  2. public class Circle {  
  3.  int x;  
  4.  int y;  
  5.  double r;  
  6.  Circle(){ //无参构造方法  
  7.   x=1;  
  8.   y=1;  
  9.   r=1.0;  
  10.  }  
  11.  Circle(int a,int b,int R){//有参构造方法  
  12.   x=a;  
  13.   y=a;  
  14.   r=R;    
  15.  }  
  16.  void Chongdie(Circle c) {  //判定重叠  
  17.   if(x==c.x&&y==c.y&&r==c.r)  
  18.    System.out.println("c1和c2重叠");  
  19.   else  
  20.    System.out.println("c1和c2不重叠");  
  21.  }  
  22. }  
  23.   
  24. import java.util.Scanner;  
  25.   
  26. class CircleTest {  
  27.      public static void main(String[] args) {  
  28.       Scanner input=new Scanner(System.in);  
  29.       int a=input.nextInt();  
  30.       int b=input.nextInt();  
  31.       int R=input.nextInt();  
  32.       Circle c1=new Circle();  
  33.       Circle c2=new Circle(a,b,R);  
  34.       c1.Chongdie(c2);  
  35.      }  
  36.     }  
4.编写代码模拟手机与SIM卡的组合关系。

要求:

           SIM卡类负责创建SIM卡;

           Phone类负责创建手机;

          手机可以组合一个SIM卡;

          手机可以更换其中的SIM卡

  1. public class Phone {  
  2.     SIM sim=new SIM();  
  3.   
  4.     public SIM getSim() {  
  5.         return sim;  
  6.     }  
  7.     public void setSim(SIM sim) {  
  8.         this.sim = sim;  
  9.     }  
  10.     public void show(){  
  11.         System.out.println(sim.getNumber());  
  12.     }  
  13.   
  14. }  
  15. public class SIM {  
  16.     int number;  
  17.     public long getNumber() {  
  18.         return number;  
  19.     }  
  20.     public void setNumber(int number) {  
  21.         this.number = number;  
  22.     }  
  23. }  
  24. public class zuhe {  
  25.   
  26.     public static void main(String[] args) {  
  27.         // TODO Auto-generated method stub  
  28.   
  29.         SIM sim=new SIM();  
  30.         sim.setNumber(123456789);  
  31.         Phone phone=new Phone();  
  32.         phone.setSim(sim);  
  33.         phone.show();  
  34.         System.out.println("换号");  
  35.         SIM simnew=new SIM();  
  36.         simnew.setNumber(987654321);  
  37.         phone.setSim(simnew);  
  38.         phone.show();  
  39.     }  
  40.   
  41. }  
5.用类描述CPU硬盘

  1. public class CPU {  
  2.     int speed;  
  3.   
  4.     public int getSpeed() {  
  5.         return speed;  
  6.     }  
  7.   
  8.     public void setSpeed(int speed) {  
  9.         this.speed = speed;  
  10.     }  
  11. }  

  1. public class HardDisk {  
  2.     int amount;  
  3.   
  4.     public int getAmount() {  
  5.         return amount;  
  6.     }  
  7.   
  8.     public void setAmount(int amount) {  
  9.         this.amount = amount;  
  10.     }  
  11.       
  12.   
  13. }  

  1. public class PC {  
  2.     CPU cpu;  
  3.     HardDisk HD;  
  4.     public CPU getCpu() {  
  5.         return cpu;  
  6.     }  
  7.     public void setCpu(CPU cpu) {  
  8.         this.cpu = cpu;  
  9.     }  
  10.     public HardDisk getHD() {  
  11.         return HD;  
  12.     }  
  13.     public void setHD(HardDisk hD) {  
  14.         this.HD = hD;  
  15.     }  
  16.     void Show() {  
  17.         System.out.println("CPU频率"+cpu.getSpeed());  
  18.         System.out.println("硬盘容量"+HD.getAmount());  
  19.     }  
  20.           
  21. }  

  1. package org.gerrysu.Test5;  
  2.   
  3. public class Test {  
  4.   
  5.     public static void main(String[] args) {  
  6.         // TODO Auto-generated method stub  
  7.         CPU cpu = new CPU();  
  8.         cpu.setSpeed(2200);  
  9.         HardDisk disk = new HardDisk();  
  10.         disk.setAmount(200);  
  11.         PC pc = new PC();  
  12.         pc.setCpu(cpu);  
  13.         pc.setHD(disk);  
  14.         pc.Show();  
  15.   
  16.     }  
  17.   
  18. }