面向对象-练习-2021-01-31-01

package com.user.test4;

import org.junit.Test;

public class OopTest1 {

	@Test
	public void testStudent() {
		Student stu = new Student("张三", "男性", 21, 1001, 89);
		System.out.println(stu.say());
		stu.setGender("女性");
		System.out.println(stu.say());
	}

	@Test
	public void testhw() {
		Husband h = new Husband("张三", 22);
		Wife w = new Wife("李四", 21);
		System.out.println(h.getinfo(w));
		w.setAge(30);
		w.setName("王五");
		System.out.println(h.getinfo(w));
		h.setName("张三2");
		h.setAge(23);
		System.out.println(w.getInfo(h));
	}

	@Test
	public void testBank() {
		Account acc = new Account(1001, 2000);
		System.out.println(acc.getInfo());
		Customer cus = new Customer("张三", 001, "13512341234", "北京", acc);
		System.out.println(cus.say());
		if (acc.withdraw(1000)) {
			System.out.println("取钱成功,余额为:" + acc.getBalance());
		} else {
			System.out.println("取钱失败,余额为:" + acc.getBalance());
		}
		if (acc.save(500)) {
			System.out.println("存钱成功,余额为:" + acc.getBalance());
		} else {
			System.out.println("存钱失败,余额为:" + acc.getBalance());
		}
		Account acc1 = new Account(1002, 2000);
		System.out.println(acc1.getInfo());
		Customer cus1 = new Customer("李四", 002, "13512341234", "上海", acc1);
		System.out.println(cus1.say());
	}

	@Test
	public void testCylinder() {
		Cylinder c1 = new Cylinder(2, 8);
		c1.printDetails();
		c1.setRadius(6);
		System.out.println(c1.getArea());
		c1.printDetails();
	}

	@Test
	public void testgc() throws Exception {
		TestGC gc1 = new TestGC();
		gc1.gc();
	}

	@Test
	public void testBox() {
		Box b1 = new Box(2, 3, 6);
		System.out.println(b1.area());
		b1.setB(9);
		System.out.println(b1.area());
	}

	@Test
	public void testcircle() {
		Circle c = new Circle(6);
		c.Perimeter();
		c.Area();
		c.setR(5);
		c.Area();
	}

	@Test
	public void testDog() {
		Dog d = new Dog("小强", "黑色", 2);
		d.show();
		d.setName("小花");
		d.show();
	}

	@Test
	public void testScanPoint() {
		ScanPoint s = new ScanPoint(2, 3);
		System.out.println(s.nowLocate());
		System.out.println(s.otherPoint(3, 4));
		System.out.println(s.showlocate());
	}

	@Test
	public void testPerson() {
		Person p = new Person("张三", "男", 23);
		p.showMe();
		p.setAge(20);
		p.showMe();
		Person p1 = new Person("李四", "男", 23);
		p1.showMe();
	}

	@Test
	public void testCar() {
		Car c1 = new Car("捷达", 1.8, "白色");
		Car c2 = new Car("宝马", 1.9, "黑色");
		Car c3 = new Car("劳斯莱斯", 2.0, "黑色");
		Car c4 = new Car("科鲁兹", 1.9, "红色");
		Car c5 = new Car("迈锐宝", 1.7, "蓝色");
		c1.showCar();
		c2.showCar();
		c3.showCar();
		c4.showCar();
		c5.showCar();
	}

	@Test
	public void testCourse() {
		course c1 = new course("c语言", 120, "白色");
		course c2 = new course("java编程", 100, "黑色");
		course c3 = new course("php网络编程", 80, "黑色");
		course c4 = new course("c++", 110, "红色");
		course c5 = new course("数据结构", 50, "蓝色");
		c1.showinfo();
		c2.showinfo();
		c3.showinfo();
		c4.showinfo();
		c5.showinfo();
	}

}

/**
 * 编写一个Student类,包含name、gender、age、id、score属性,分别为String、String、int、int、double类型。
 * 类中声明一个say方法,返回String类型,方法返回信息中包含所有属性值。
 * 
 * @author Administrator
 *
 */

class Student {
	private String name;
	private String gender;
	private int age;
	private int id;
	private double scroe;

	public Student() {
		super();
	}

	public Student(String name, String gender, int age, int id, double scroe) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.id = id;
		this.scroe = scroe;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public double getScroe() {
		return scroe;
	}

	public void setScroe(double scroe) {
		this.scroe = scroe;
	}

	public String say() {
		return "姓名:" + this.name + "\t性别:" + this.gender + "\t年龄:" + this.age + "\t身份ID:" + this.id + "\t分数:"
				+ this.scroe;
	}
}

/**
 * 定义一个丈夫Husband类,有姓名、年龄、妻子属性定义一个妻子Wife类,有姓名、年龄、丈夫属性
 * 丈夫类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和他的妻子的姓名,年龄
 * 妻子类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和她的丈夫的姓名,年龄
 * 
 * @author Administrator
 *
 */
class Husband {
	private String name;
	private int age;

	public Husband() {
		super();
	}

	public Husband(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getinfo(Wife wife) {
		return "丈夫名字:" + this.name + ",丈夫年龄:" + this.age + ",妻子名字:" + wife.getName() + ",妻子年龄" + wife.getAge();
	}

}

class Wife {
	private String name;
	private int age;

	public Wife() {
		super();
	}

	public Wife(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getInfo(Husband husband) {
		return "妻子名字:" + this.name + ",妻子年龄:" + this.age + ",丈夫名字:" + husband.getName() + ",丈夫年龄" + husband.getAge();

	}
}

class Account {
	private int cid;
	private double balance;

	public Account() {
		super();
	}

	public Account(int cid, double balance) {
		super();
		this.cid = cid;
		this.balance = balance;
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public String getInfo() {
		return "卡号是:" + this.cid + "\t余额为:" + this.balance;
	}

	public boolean withdraw(double draw) {
		if (this.balance < draw) {
			return false;
		}
		this.balance -= draw;
		return true;
	}

	public boolean save(double savemoney) {
		if (savemoney < 0) {
			return false;
		}
		this.balance += savemoney;
		return true;
	}
}

class Customer {
	private String name;
	private int id;
	private String telnumber;
	private String adress;
	private Account Acc;

	public Customer() {
		super();
	}

	public Customer(String name, int id, String telnumber, String adress, Account acc) {
		super();
		this.name = name;
		this.id = id;
		this.telnumber = telnumber;
		this.adress = adress;
		this.Acc = acc;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTelnumber() {
		return telnumber;
	}

	public void setTelnumber(String telnumber) {
		this.telnumber = telnumber;
	}

	public String getAdress() {
		return adress;
	}

	public void setAdress(String adress) {
		this.adress = adress;
	}

	public Account getAcc() {
		return Acc;
	}

	public void setAcc(Account acc) {
		Acc = acc;
	}

	public String say() {
		return "姓名:" + this.name + "\t身份ID是:" + this.id + "\t电话号码:" + this.telnumber + "\t地址是:" + this.adress
				+ Acc.getInfo();
	}
}

class Cylinder {
	private double radius;
	private double height;
	double C = Math.PI;

	public Cylinder() {
		super();
	}

	public Cylinder(double radius, double height) {
		super();
		this.radius = radius;
		this.height = height;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public void printDetails() {
		System.out.println("圆柱体的底边的半径是" + radius + " ,高是" + height + ",底面积是" + getArea() + ",体积是" + getVolume() + "。");
	}

	double getArea() {
		return 2 * C * this.radius;
	}

	double getVolume() {
		return getArea() * height;
	}
}

/**
 * 分配:由JVM自动为其分配相应的内存空间 释放:由JVM提供垃圾回收机制自动的释放内存空间 垃圾回收机制(GC:Garbage
 * Collection):将垃圾对象所占用的堆内存进行回收。Java的垃圾回收机制是JVM提供的能力,由单独的系统级垃圾回收线程在空闲时间以不定时的方式动态回收。
 * 垃圾对象:不再被任何引用指向的对象。 面试题: 问:在程序中是否可以通知垃圾回收机制过来回收垃圾?
 * 能,通过调用System.gc();或Runtime.getRuntime().gc();
 * 再问:调用了System.gc();或Runtime.getRuntime().gc();后是立刻执行垃圾回收吗?
 * 不是,该调用并不会立刻启动垃圾回收机制开始回收,但会加快垃圾回收机制的运行。
 * 
 * @author Administrator
 *
 */
class TestGC {
	public void gc() throws Exception {
		for (int i = 0; i < 10; i++) {
			MyClass m = new MyClass();// 这里本次循环完,本次创建的对象就成为垃圾了
			System.out.println("创建第" + (i + 1) + "的对象:" + m);
		}
		// 通知垃圾回收机制来收集垃圾
		System.gc();
		// 为了延缓程序结束
		for (int i = 0; i < 10; i++) {
			Thread.sleep(1);
			System.out.println("程序在继续....");
		}
	}
}

class MyClass {
	// 这个方法是垃圾回收机制在回收它的对象时,自动调用,理解成对象留临终遗言的方法
	public void finalize() {
		System.out.println("MyClass.....");
	}
}

class Box {
	private double a;
	private double b;
	private double c;

	public Box() {
		super();
	}

	public Box(int a, int b, int c) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
	}

	public void setA(double a) {
		this.a = a;
	}

	public double getA() {
		return this.a;
	}

	public double getB() {
		return b;
	}

	public void setB(double b) {
		this.b = b;
	}

	public double getC() {
		return c;
	}

	public void setC(double c) {
		this.c = c;
	}

	public String area() {
		return "长=" + a + "\t宽= " + b + "\t高= " + c + "\t体积= " + (a * b * c);
	}
}

/**
 * 提供显示圆周长功能的方法 提供显示圆面积的方法 提供无参的构造器和一个有参的构造器
 * 
 * @author Administrator
 *
 */
class Circle {
	private double r;
	private double p = Math.PI;

	public Circle() {
		super();
	}

	public Circle(double r) {
		super();
		this.r = r;
	}

	public void setR(double r) {
		this.r = r;
	}

	public double getR() {
		return this.r;
	}

	public void Perimeter() {
		double z = 2 * this.p * this.r;
		System.out.println("半径是:" + this.r + "\t周长是:" + z);
	}

	public void Area() {
		double c = this.p * this.r * this.r;
		System.out.println("半径是:" + this.r + "\t面积是:" + c);
	}

}

/**
 * 设计一个Dog类,有名字、颜色和年龄属性,定义构造器初始化这些属性,定义输出方法show()显示其信息。
 * 
 * @author Administrator
 *
 */
class Dog {
	private String name;
	private String colour;
	private int age;

	public Dog() {
		super();
	}

	public Dog(String name, String colour, int age) {
		super();
		this.name = name;
		this.colour = colour;
		this.age = age;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setColour(String colour) {
		this.colour = colour;
	}

	public String getColour() {
		return this.colour;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getAge() {
		return this.age;
	}

	public void show() {
		System.out.println("名字:" + this.name + "\t顔色是:" + this.colour + "\t年齡是:" + this.age);
	}
}

class ScanPoint {
	private int x;
	private int y;

	public ScanPoint() {
		super();
	}

	public ScanPoint(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public String nowLocate() {
		return "x= " + this.x + "\ty= " + this.y + "\t到原点的距离是:" + (Math.sqrt(x * x + y * y));
	}

	public String otherPoint(int x1, int y1) {
		return "x= " + this.x + "\ty= " + this.y + "到" + "x= " + x1 + "\ty= " + y1 + "的距离是:" + getDistance(x1, y1);
	}

	double getDistance(int x1, int y1) {
		double r = Math.sqrt(x1 - x) * (x1 - x) + (y1 - y) * (y1 - y);
		return r;
	}

	public String showlocate() {
		return "x=" + this.x + "\ty= " + this.y;
	}
}

class Person {
	private String name;
	private String gender;
	private int age;

	public Person() {
		super();
	}

	public Person(String name, String gender, int age) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void showMe() {
		System.out.println("姓名是: " + this.name + "\t性别是:" + this.gender + "\t年龄是:" + this.age + "\t在" + eat());
	}

	String eat() {
		return "吃饭!!!";
	}
}

class Car {
	private String brand;
	private double carlong;
	private String colour;

	public Car() {
		super();
	}

	public Car(String brand, double carlong, String colour) {
		super();
		this.brand = brand;
		this.carlong = carlong;
		this.colour = colour;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public double getCarlong() {
		return carlong;
	}

	public void setCarlong(double carlong) {
		this.carlong = carlong;
	}

	public String getColour() {
		return colour;
	}

	public void setColour(String colour) {
		this.colour = colour;
	}

	public void showCar() {
		System.out.println("品牌:" + this.brand + "\t颜色:" + this.colour + "\t车厂:" + this.carlong);
	}
}

class course {
	private String coursename;
	private int hours;
	private String teacher;

	public course() {
		super();
	}

	public course(String coursename, int hours, String teacher) {
		super();
		this.coursename = coursename;
		this.hours = hours;
		this.teacher = teacher;
	}

	public String getCoursename() {
		return coursename;
	}

	public void setCoursename(String coursename) {
		this.coursename = coursename;
	}

	public int getHours() {
		return hours;
	}

	public void setHours(int hours) {
		this.hours = hours;
	}

	public String getTeacher() {
		return teacher;
	}

	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}

	public void showinfo() {
		System.out.println("课程名称是:" + this.coursename + "\t学时:" + this.hours + "\t任课老师:" + this.teacher);
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值