Java_2017.10.09



1.定义一个关于交通的类。

在Vehicle类中

package Occ;

public class Vehicle {

	double speed;
	double size;

	public void move() {
		System.out.println("要加油我才能走");
	}

	public void setSpeed(int speed) {
		System.out.println("初始化速度为" + speed);
	}

	public void speedUp() {
		speed += 111;
		System.out.println("已加速为" + speed);

	}

	public void speedDown() {
		speed -= 11;
		System.out.println("已减速为" + speed);

	}

}
在VehicleTest测试类中
package Occ;


public class VehicleTest {

	public static void main(String[] args) {

		Vehicle v = new Vehicle();
		v.speed = 111;
		v.size = 22.2;
		v.move();
		v.setSpeed(111);
		v.speedUp();
		v.speedDown();
	}
}



/*
要加油我才能走
初始化速度为111
已加速为222.0
已减速为211.0
*/


可以直接创建对象,再给对象赋值。等价于定义一个有参的构造方法。

2.定义一个梯形类

package work01;

public class Tixing {

	double upper, lower, high;

	public Tixing(double upper, double lower, double high) {
		this.upper = upper;
		this.lower = lower;
		this.high = high;
	}

	double getArea() {
		return (upper + lower) * high / 2;
	}

	public double getUpper() {
		return upper;
	}

	public void setUpper(double upper) {
		this.upper = upper;
	}

	public double getLower() {
		return lower;
	}

	public void setLower(double lower) {
		this.lower = lower;
	}

	public double getHigh() {
		return high;
	}

	public void setHigh(double high) {
		this.high = high;
	}

	public String toString() {
		return "Tixing [upper=" + upper + ", lower=" + lower + ", high=" + high
				+ "]";
	}
}


package work01;

public class TestTixing {

	public static void main(String[] args) {
		Tixing t1 = new Tixing(10, 20, 10);
		System.out.println(t1.upper + " " + t1.lower + " " + t1.high);
		System.out.println(t1.getArea());
		System.out.println(t1.toString());
		t1.setHigh(100);
		System.out.println(t1.toString());
		Tixing t2 = new Tixing(20, 40, 20);
		System.out.println(t2.toString());
		t2 = t1;
		System.out.println(t2.toString());
		System.out.println(t1.toString());
		t1.setHigh(50);
		System.out.println(t2.toString());
		System.out.println(t1.toString());
		// 简单变量赋值后处理
		int i = 10;
		int j = i;
		System.out.println(i + " " + j);
		i = 20;
		System.out.println(i + " " + j);
	}
}

/*
10.0 20.0 10.0
150.0
Tixing [upper=10.0, lower=20.0, high=10.0]
Tixing [upper=10.0, lower=20.0, high=100.0]
Tixing [upper=20.0, lower=40.0, high=20.0]
Tixing [upper=10.0, lower=20.0, high=100.0]
Tixing [upper=10.0, lower=20.0, high=100.0]
Tixing [upper=10.0, lower=20.0, high=50.0]
Tixing [upper=10.0, lower=20.0, high=50.0]
10 10
20 10
*/

3.定义一个学生类

编程定义一个学生类包括成员变量 xm(姓名)、xh(学号)、xb(性别);成员方法有构造方法(不带参数和带三个参数),设置和获取上述三个成员变量的方法,以及打印出姓名、学号与性别的print()方法、toString()方法。


package work01;

public class Student {

	String xm,xh,xb;
	public Student(){
		
	}
	public Student(String xm,String xh,String xb){
		this.xm=xm;
		this.xh=xh;
		this.xb=xb;
	}
	public void setXm(String xm){
		this.xm=xm;
	}
	public String getXm(){
		return xm;
	}
	public void setXh(String xh){
		this.xh=xh;
	}
	public String getXh(){
		return xh;
	}
	public void setXb(String xb){
		this.xb=xb;
	}
	public String getXb(){
		return xb;
	}
	public String toString(){
		return "(toString)姓名:"+xm+" 学号:"+xh+" 性别:"+xb;
		
	}
	public static void main(String[] args) {
		Student stu0=new Student();
		stu0.xm="java";
		Student stu=new Student("wxy","222","女");
		System.out.println(stu0.toString());
		System.out.println("姓名:"+stu.xm+" 学号:"+stu.xh+" 性别:"+stu.xb);
		System.out.println(stu.toString());
		stu.setXh("214");
		stu.setXm("lm");
		stu.setXb("男");
		System.out.println(stu.toString());
	}
}

/*
(toString)姓名:java 学号:null 性别:null
姓名:wxy 学号:222 性别:女
(toString)姓名:wxy 学号:222 性别:女
(toString)姓名:lm 学号:214 性别:男
*/


4.定义一个复数类

package work01;

public class Complex {

	double real, ima;

	public Complex() {

	}

	public Complex(double real, double ima) {
		this.real = real;
		this.ima = ima;
	}

	public double getReal() {
		return real;
	}

	public double getIma() {
		return ima;
	}

	public void setReal(double real) {
		this.real = real;
	}

	public void setIma(double ima) {
		this.ima = ima;
	}

	public Complex add(Complex c) {
		double newreal = this.real + c.getReal();
		double newima = this.ima + c.getIma();
		Complex ans = new Complex(newreal, newima);
		return ans;
	}

	public Complex sub(Complex c) {
		double newreal = this.real - c.getReal();
		double newima = this.ima - c.getIma();
		Complex ans = new Complex(newreal, newima);
		return ans;
	}

	public String toString() {
		if (this.ima == 0) {
			return "" + this.real;
		} else if (this.ima > 0) {
			return this.real + "+" + this.ima + "i";
		} else {
			return this.real + "" + this.ima + "i";
		}
	}

	public static void main(String[] args) {
		Complex a = new Complex(1, 2);
		Complex b = new Complex(9, 9);
		System.out.println(a.add(b));
		System.out.println(a.sub(b));
	}
}

/*
 * 10.0+11.0i 
 * -8.0-7.0i
 */



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值