类与对象

1、类的声明

/*
	类型-》class声明
	类是对象的抽象,对象是类的具体表现形式。

	属性-》成员变量
	行为-》方法
*/

public class Person {
	String name;
	int age;
	int height;
	int weight;

	public static void main(String[] args) {
		int x = 1;
		//声明Person类型的变量,并创建Person类型的对象。
		Person p = new Person();
		//访问对象的成员(引用.成员名)
		p.name = "张三";
		p.age = 10;
		p.height = 100;
		p.weight = 50;
		//仅仅只是声明了引用类型的变量,并没有创建
		//真正的对象。真正的对象是通过new来创建的。
		Person p2;
	}
}

  2、成员变量

/*
	成员变量
	直接声明在类的内部的变量称为成员变量。
	成员变量的作用域为整个类。

	如果局部变量与成员变量同名,则在局部变量的作用域内,
	局部变量将会遮蔽同名的成员变量。
	如果想访问被局部变量所遮蔽的成员变量,可以通过this
	进行访问。
*/
public class Member {
	//成员变量
	int x = 1;

	public void f() {
		//局部变量
		int x = 2;
		//访问局部变量x
		System.out.println(x);
		//访问成员变量x
		System.out.println(this.x);
	}
	
	public void g() {
		//访问成员变量x。
		System.out.println(x);
	}
	
	public static void main(String[] args) {
		Member m = new Member();
		m.f();
		//m.g();
	}
}

 3、方法

/*
	方法
	访问权限 返回类型 方法名(参数列表) {
		方法体
	}
	
	参数列表:
	参数类型1 参数名1,参数类型2 参数名2, ……
	参数类型n 参数名n

	如果方法没有返回值,使用void。
	
	程序设计原则:
	不要让相同的代码重复出现。原因:
	1 重复的代码会造成代码量膨胀,冗余。
	2 重复的代码不利于程序的维护。(程序更新时,
	所有重复的代码都需要更新)

	使用方法的优势:
	避免代码的重复,实现代码的重用。

	当调用一个方法时,程序流程就会跳转到方法声明处,依次
	执行方法体语句,当所有语句执行完毕时,程序流程会返回
	到方法的调用端。继续执行方法调用之后的语句。
*/

public class Method {
	public static void main(String[] args) {
		/*
		//过于繁琐,不妥。
		System.out.println("*");
		System.out.println("**");
		System.out.println("***");
		System.out.println("****");
		System.out.println("*****");*/
		
		/*
			使用循环来进行输出,有了一定的改善。
			但是,依然会带来代码重复。
		*/
		/*for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
		//其他操作
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}*/
		Method m = new Method();
		//调用方法
		m.printStar();
		//其他的操作
		m.printStar();
	}

	public void printStar() {
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

  

/*
	含有参数的方法
	在方法声明中的参数称为形式参数。
	在方法调用中的参数称为实际参数。
	在方法调用时,实际参数会依次的赋值给
	对应的形式参数。
*/

public class Method2 {
	public void compute(int a, int b) {
		//int a = 10;
		//int b = 5;
		System.out.println(a + b);
		System.out.println(a - b);
		System.out.println(a * b);
		System.out.println(a / b);
	}
	/*
	public void compute() {
		int a = 20;
		int b = 10;
		System.out.println(a + b);
		System.out.println(a - b);
		System.out.println(a * b);
		System.out.println(a / b);
	}
	*/

	public static void main(String[] args) {
		Method2 m = new Method2();
		//byte s = 30;
		m.compute(2, 3);
		m.compute(10, 5);
		//m.cpmpute(s, 10);
	}
}

  

/*
	方法的返回值。
	return 会令方法提前结束。
	return后的语句不会得到执行。

	当方法没有返回类型(void)时,
	也可以使用return。此时return后
	不能有具体值。

	return可以返回一个值,当我们需要方法能够
	返回多个值时,可以使用数组(集合)来实现。
*/
public class Method3 {
	public int add(int a, int b) {
		int sum = a + b;
		//使用return返回一个值。
		/*if (a > 10) {
			return sum;
		} else {
			return 30;
		}*/
		//System.out.println("****");
		//return 100;
		return sum;
		//System.out.println("***");

		//return a + b;
	}
	//返回类型为数组类型,这样就可以返回多个值。
	public int[] compute(int a, int b) {
		/*return a + b;
		return a - b;
		return a * b;
		return a / b;
		*/
		int[] result = new int[4];
		result[0] = a + b;
		result[1] = a - b;
		result[2] = a * b;
		result[3] = a / b;
		return result;
	}

	public void f(int age) {
		if (age <= 0) {
			return;
		}
		//对年龄进行的操作。
	}

	public static void main(String[] args) {
		Method3 m = new Method3();
		//int x = m.add(1, 1);
		int[] y = m.compute(10, 5);
		//变量y与compute方法中的result指向了同一个数组
		//对象。在compute方法结束后,方法中创建的数组对象
		//还可以继续使用。
		System.out.println(y[0]);
		//return;
	}
}

  4、变量的默认值

 

/*
	变量的默认值
	成员变量具有默认值。布尔类型为false,数值类型为0
	(整数类型byte,short,int,long为0,float,double为0.0),
	char为'\u0000',引用类型为null。

	局部变量没有默认值,如果尝试去使用一个尚未初始化的
	局部变量,将会产生编译错误。(如果仅声明,但不使用
	尚未初始化的局部变量,没有问题。)

	数组元素如果没有显式的初始化,也具有默认值,默认值与
	对应类型的成员变量一致。
*/
public class DefaultValue {
	boolean bool;
	byte b;
	short s;
	int i;
	long l;
	char c;
	float f;
	double d;
	String str;
	int[] array;

	public static void main(String[] args) {
		DefaultValue v = new DefaultValue();
		System.out.println(v.bool);
		System.out.println(v.b);
		System.out.println(v.s);
		System.out.println(v.i);
		System.out.println(v.l);
		System.out.println(v.c);
		System.out.println(v.f);
		System.out.println(v.d);
		System.out.println(v.str);
		int x;
		//错误,局部变量没有默认值。
		//System.out.println(x);
		System.out.println(v.array);
		v.array = new int[5];
		System.out.println(v.array[0]);
	}
}

  5、NULL值

/*
	null值
	null是引用类型的字面常量值。null可以赋值给
	任意的引用类型。表示该引用变量没有指向有效
	的对象。

	如果一个引用变量为null,我们通过该引用变量
	去访问类中声明的成员,将会在运行时产生
	NullPointerException异常(编译时没有错误)。
*/
public class Null {
	public static void main(String[] args) {
		String s = null;
		int[] x = null;
		//错误,运行时会产生异常。
		System.out.println(x.length);
	}
}

  6、创建对象

/*
	通过new创建对象,分配在堆内存中。
	每通过new创建一个对象,都会在堆中分配一块独立的
	空间,这意味着,每个对象都有自己的成员变量(实例),
	彼此之间不受干扰。如果一个对象改变了自己成员变量的值,
	不会对其他对象造成影响。
*/
public class CreateObject {
	int x = 1;
	public static void main(String[] args) {
		CreateObject o1 = new CreateObject();
		CreateObject o2 = new CreateObject();
		System.out.println(o1.x);
		System.out.println(o2.x);
		o1.x = 100;
		System.out.println(o1.x);
		//o2对象的成员变量不会因为o1对象的改变而改变。
		System.out.println(o2.x);
	}
}

  7、参数传递

/*
	参数传递
	1 参数类型是基本数据类型
	2 参数类型是引用类型


	当参数是基本数据类型时,形参参数(形参)的改变
	不会反作用于实际参数(实参)。
*/



public class Pass {
	public static void main(String[] args) {
		Pass p = new Pass();
		int x = 1;
		int y = 2;
		p.swap(x, y);
		System.out.println(x);
		System.out.println(y);
	}

	public void swap(int a, int b) {
		//a = x;
		//b = y;
		int temp = a;
		a = b;
		b = temp;
		System.out.println(a);
		System.out.println(b);
	}
}

  

/*
	参数是引用类型时的参数传递

	当参数类型是引用类型时,通过形参可以改变实参
	指向对象的值(对象的成员变量)。
	因为实参与形参指向了同一个对象。
*/

public class Pass2 {
	public static void main(String[] args) {
		Value v1 =new Value();
		v1.v = 1;
		Value v2 = new Value();
		v2.v = 2;
		Pass2 p = new Pass2();
		p.swap(v1, v2);
		System.out.println(v1.v);
		System.out.println(v2.v);

	}

	public void swap(Value x, Value y) {
		int temp = x.v;
		x.v = y.v;
		y.v = temp;
	}
}

class Value {
	int v;
}

  

/*
	参数传递总结:
	Java中,无论参数是基本数据类型,还是引用类型,传递
	的方式一律是按值传递。这就是说,形式参数的改变,无法
	影响实际参数。
	对于参数是引用类型时,通过形参可以改变实参所指向对象
	的值,但是,依然无法改变实参本身的值。(对象的起始地址)。
*/

public class Pass3 {
	public static void main(String[] args) {
		Value v1 =new Value();
		v1.v = 1;
		Value v2 = new Value();
		v2.v = 2;
		Pass3 p = new Pass3();
		p.swap(v1, v2);
		System.out.println(v1.v);
		System.out.println(v2.v);

	}

	public void swap(Value x, Value y) {
		x = new Value();
		x.v = 2;
		y = new Value();
		y.v = 1;
	}
}

  

 

posted @ 2017-03-17 21:48  凌-风  阅读(239)  评论(0编辑  收藏  举报