马士兵J2SE-第三章-面向对象-基础及重载

面向对象:

//面向对象的方法写一段程序,要求能够手动修改年月日的信息


class Date {
	private int day;
	private int month;
	private int year;
	
	public Date (int d, int m, int y) {
		day = d; 
		month = m;
		year = y;
	}
	
	public void setDay(int d) {
		day=d;
	}
	
	public void setMonth(int m) {
		month = m;
	}
	
	public void setYear(int y) {
		year = y;
	}
	
	public int getDay() {
		return day;
	}
	
	public int getMonth() {
		return month;
	}
	
	public int getYear() {
		return year;
	}
	
	public void display() {
		System.out.println(day+"-"+month+"-"+year);
	}
}

public class test {
	public static void main(String[] args) {
		test testdemo = new test();
		Date d1=new Date(3,11,2011);
		testdemo.change(d1);
		d1.display();
		
	}
	
	public void change(Date b) {
		b.setDay(5);
		b.setMonth(6);
		b.setYear(2046);
	}
	
}


输出:

5-6-2046


我的写法,不过有问题,计算SQRT应该是DOUBLE的,所以定义的时候就有问题,但是毕竟是新手,依葫芦画瓢,能实现功能就。。。。对JAVA的使用也不灵活,死板的用吧。。。。

//定义一个"点"(Point)类来表示三维空间的点(有三个坐标)
//1.可以生成具有特定坐标的点对象
//2.提供那个可以设置三个坐标的方法
//3.提供可以计算该点距远点距离平方的方法
import java.math.*;
class Point {
	private int xl;
	private int yl;
	private int zl;
	
	public Point(int x,int y,int z) {
		xl = x;
		yl = y;
		zl = z;
	}
	
	public void setXL(int x) {
		xl = x;
	}
	
	public void setYL(int y) {
		yl = y;
	}
	
	public void setZL(int z) {
		zl = z;
	}
	
	public int getXL() {
		return xl;
	}
	
	public int getYL() {
		return yl;
	}
	
	public int getZL() {
		return zl;
	}
	
	public void showlocation() {
		System.out.println(xl+","+yl+","+zl);
	}
	
	public void calclocation() {
		double n=xl*xl+yl*yl+zl*zl;
		double m=Math.sqrt(n);
		System.out.println(m);
	}

	
}

public class test {
	public static void main(String[] args) {
		test testdemo = new test();
		Point p1=new Point(3,4,0);
	//	testdemo.change(p1);
		p1.showlocation();
		p1.calclocation();
	}
	
	public void change(Point d) {
		d.setXL(12);
		d.setYL(14);
		d.setZL(18);
	}
}

输出:

3,4,0
5.0


马士兵的写法

class Point {
	double x,y,z;
	
	Point (double _x,double _y,double _z) {
		x = _x;
		y = _y;
		z = _z;
	}
	
	void setX(double _x) {
		x = _x;
	}
	
	void setY(double _y) {
		y = _y;
	}
	
	void setZ(double _z) {
		z = _z;
	}
	
	double getDistance(Point p) {
		return (x - p.x)*(x - p.x) +(y - p.y)*(y-p.y)+(z-p.z)*(z-p.z);
	}
	
	
}

    public class test {
    	public static void main(String[] args) {
    		Point p =new Point (1.0,2.0,3.0);
    		Point p1=new Point (0.0,0.0,0.0);
    		System.out.println(p.getDistance(p1));
    		
    		p.setX(5.0);
    		System.out.println(p.getDistance(new Point(1.0,1.0,1.0)));
    	}
    }



重载

public class test {

	public static void main(String[] args) {
		test t=new test();
		t.max(3, 4);
		short a=3;
		short b=4;
		t.max(a, b);
	}
	
	void max(short a,short b) {
		System.out.println("short");
		System.out.println(a > b?a:b);
	}
	
	void max(int a, int b) {
		System.out.println("int");
		System.out.println( a > b ? a : b);
	}
 
	void maxFlaot(float a, float b) {
		System.out.println("float");
		System.out.println( a> b ? a: b);
	}
	
	void max(float a,float b) {
		System.out.println( a > b ? a: b);
	}
	

 
}


输出:

int
4
short
4


 

this

注意分析内存

public class test {
	int i = 0;
	
	test(int i) {
		this.i=i;
	}
	
	test increament() {
		i++;
		return this; 
	}
	
	void print() { 
		System.out.println("i="+i);
	};
	
	public static void main(String[] args) {
		test testdemo=new test(100);
		testdemo.increament().increament().print();
	}
}


输出:

i=102

 

 

//为circle类建立一个方法,计算一个点(Point对象)是否在圆(Circle对象)内
class Point {
	private double x;
	private double y;
	
	Point(double x1,double y1) {
		x=x1;
		y=y1;
	}
	

	public void setX(double x1) {
		x=x1;
	}
	
	public void setY(double y1) {
		y = y1;
	}
	
	public double getX() {
		return x;
	}
	
	public double getY() {
		return y;
	}
	
}


class Circle {
	private Point o;//点,圆心
	private double radius;//半径
	
	Circle(Point p,double r) {
		o = p;
		r = radius;
	}
	
	Circle(double r) {
		o = new Point(0.0,0.0);
		radius=r;
	}

	public void setO(double x,double y) {
		o.setX(x);
		o.setY(y);
	}
	
	public void setRadius(double r) {
		radius = r;
	}
	public Point getO() {
		return o;
	}
	
	public double getRadius() {
		return radius;
	}
	
	public double area() {
		return 3.14*radius*radius;
	}
	
	boolean contains(Point p) {
		double x=p.getX()-o.getX();
		double y=p.getY()-o.getY();
		
		if(x*x+y*y>radius*radius)
			return false;
		else return true;
	}
	
}

public class test {
	public static void main(String[] args) {
		Circle c1=new Circle(new Point(1.0,2.0),2.0);
		Circle c2=new Circle(5.0);
		System.out.println("c1:("+c1.getO().getX()+","+c1.getO().getY()+"),"+c1.getRadius());
		System.out.println("c1:("+c2.getO().getX()+","+c2.getO().getY()+"),"+c2.getRadius());
		System.out.println("c1 area="+c1.area());
		System.out.println("c2 area="+c2.area());
		c1.setO(5, 6);
		c2.setRadius(9.0);
		System.out.println("c1:("+c1.getO().getX()+","+c1.getO().getY()+"),"+c1.getRadius());
		System.out.println("c1:("+c2.getO().getX()+","+c2.getO().getY()+"),"+c2.getRadius());
		System.out.println("c1 area="+c1.area());
		System.out.println("c2 area="+c2.area());
		
		Point p1 = new Point(5.2,6.3);
		System.out.println(c1.contains(p1));
		System.out.println(c2.contains(new Point(10.0,9.0)));
	}
}


输出:

 

 c1:(1.0,2.0),0.0
c1:(0.0,0.0),5.0
c1 area=0.0
c2 area=78.5
c1:(5.0,6.0),0.0
c1:(0.0,0.0),9.0
c1 area=0.0
c2 area=254.34
false
false

自己理解做了点笔记写的程序,貌似答案有点问题。。。。加油训练吧!

你可能感兴趣的:(c,Date,String,Class,import,float)