类与对象
来源:互联网 发布:阿里云oss是干什么的 编辑:程序博客网 时间:2024/06/16 04:18
1.请定义一个交通工具(Vehicle)的类,其中有:
属性:速度(speed),体积(size)等
方法:移动(move()),设置速度(setSpeed(intspeed)),设置体积(setSize(int size))加速speedUp(),减速speedDown()等
在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变。
- public class Vehicle {
- int speed;
- int size;
- public Vehicle(int speed, int size) {
- // TODO Auto-generated constructor stub
- this.speed = speed;
- this.size = size;
- }
- public int getSpeed() {
- return speed;
- }
- public void setSpeed(int speed) {
- this.speed = speed;
- }
- public int getSize() {
- return size;
- }
- public void setSize(int size) {
- this.size = size;
- }
- void move(){
- }
- int speedUp(){
- return speed+10;
- }
- int speedDown(){
- return speed-5;
- }
- }
- public class VehicleTest {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Vehicle v = new Vehicle(10,5);
- System.out.println("速度:"+v.getSpeed());
- System.out.println("体积:"+v.getSize());
- System.out.println("加速:"+v.speedUp());
- System.out.println("减速:"+v.speedDown());
- }
- }
- import java.util.Calendar;
- import java.util.Date;
- public class Time {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Date time=new Date();
- System.out.println(time);//获取时间
- Calendar cal =Calendar.getInstance();
- cal.setTime(new Date());
- int year=cal.get(Calendar.YEAR);
- int month=cal.get(Calendar.MONTH)+1; //一月对应0;
- int day=cal.get(Calendar.DAY_OF_MONTH);
- System.out.print(" "+year+"年"+" "+month+"月"+day+"日");
- }
- }
1、 编写一个无参的构造函数;
2、 编写一个有参的构造函数;
3、 在主函数中调用无参的构造函数生成圆的实例c1,调用有参的构造函数生成圆的实例c2,调用实例方法判断c1和c2是否相重叠。
- import java.util.*;
- public class Circle {
- int x;
- int y;
- double r;
- Circle(){ //无参构造方法
- x=1;
- y=1;
- r=1.0;
- }
- Circle(int a,int b,int R){//有参构造方法
- x=a;
- y=a;
- r=R;
- }
- void Chongdie(Circle c) { //判定重叠
- if(x==c.x&&y==c.y&&r==c.r)
- System.out.println("c1和c2重叠");
- else
- System.out.println("c1和c2不重叠");
- }
- }
- import java.util.Scanner;
- class CircleTest {
- public static void main(String[] args) {
- Scanner input=new Scanner(System.in);
- int a=input.nextInt();
- int b=input.nextInt();
- int R=input.nextInt();
- Circle c1=new Circle();
- Circle c2=new Circle(a,b,R);
- c1.Chongdie(c2);
- }
- }
要求:
SIM卡类负责创建SIM卡;
Phone类负责创建手机;
手机可以组合一个SIM卡;
手机可以更换其中的SIM卡
- public class Phone {
- SIM sim=new SIM();
- public SIM getSim() {
- return sim;
- }
- public void setSim(SIM sim) {
- this.sim = sim;
- }
- public void show(){
- System.out.println(sim.getNumber());
- }
- }
- public class SIM {
- int number;
- public long getNumber() {
- return number;
- }
- public void setNumber(int number) {
- this.number = number;
- }
- }
- public class zuhe {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- SIM sim=new SIM();
- sim.setNumber(123456789);
- Phone phone=new Phone();
- phone.setSim(sim);
- phone.show();
- System.out.println("换号");
- SIM simnew=new SIM();
- simnew.setNumber(987654321);
- phone.setSim(simnew);
- phone.show();
- }
- }
- public class CPU {
- int speed;
- public int getSpeed() {
- return speed;
- }
- public void setSpeed(int speed) {
- this.speed = speed;
- }
- }
- public class HardDisk {
- int amount;
- public int getAmount() {
- return amount;
- }
- public void setAmount(int amount) {
- this.amount = amount;
- }
- }
- public class PC {
- CPU cpu;
- HardDisk HD;
- public CPU getCpu() {
- return cpu;
- }
- public void setCpu(CPU cpu) {
- this.cpu = cpu;
- }
- public HardDisk getHD() {
- return HD;
- }
- public void setHD(HardDisk hD) {
- this.HD = hD;
- }
- void Show() {
- System.out.println("CPU频率"+cpu.getSpeed());
- System.out.println("硬盘容量"+HD.getAmount());
- }
- }
- package org.gerrysu.Test5;
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- CPU cpu = new CPU();
- cpu.setSpeed(2200);
- HardDisk disk = new HardDisk();
- disk.setAmount(200);
- PC pc = new PC();
- pc.setCpu(cpu);
- pc.setHD(disk);
- pc.Show();
- }
- }
阅读全文
0 0
- OOP-----类与对象对象
- OOP-----类与对象对象
- 对象与元类对象
- 对象与类类型
- C02(类与对象)
- 类与对象(PHP5)
- 类与对象
- 区分类与对象
- 类与对象
- 类与对象
- 类与对象
- C#类与对象
- 对象与类
- 类与对象总结
- js类与对象
- C++ 类与对象
- 类与对象
- 类与对象概述
- Android tostring方法
- 二元的表达式的计算(数据结构)
- 并发整理
- JavaScript 输出
- 究竟啥才是互联网架构“高可用”
- 类与对象
- 安装 .NET Framework的技巧以及探究路径
- DeepFashion: Powering Robust Clothes Recognition and Retrieval with Rich Annotations
- JS简单的数组排序
- MySQL5.6 数据库主从(Master/Slave)
- Android Study Material Design 六 之:TextInputLayout学习及分析部分源码
- 支教志愿者和支教旅行者·《乖摸摸头》·一
- [Unity][Android]打包APK的时候PackageName无法改变
- tensorflow-estimators