黑马程序员——面向对象

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

 

   还是老情况,篇幅有限,图片又大,所以拉长看。如果有人想要源文件,可以在评论中提出来。

 

 

先上数组工具类:

class ArrayToolDemo {
	/*
	 * 保证程序的独立运行。
	 */
	public static void main(String[] args) {
		int[] arr = { 4, 8, 2, 9, 72, 6 };

		// ArrayTool tool = new ArrayTool();

		// int max= ArrayT8ool.getMax(arr);
		// System.out.println("max="+max);

		int index = ArrayTool.getIndex(arr, 8);
		System.out.println("index=" + index);
	}

}

/**
 * 建立一个用于操作数组的工具类,其中包含着常见的对数组操作的函数如:最值,排序等 。
 * 
 * @author 张三
 * @version V1.0
 */

public class ArrayTool {
	private ArrayTool() {
	}// 该类中的方法都是静态的,所以该类是不需要的创建对象的。为了保证不让其他成创建该类对象
		// 可以将构造函数私有化。

	/**
	 * 获取整型数组的最大值。
	 * 
	 * @param arr 接收一个元素为int类型的数组。
	 * @return 该数组的最大的元素值
	 */
	public static int getMax(int[] arr) {
		int maxIndex = 0;
		for (int x = 1; x < arr.length; x++) {
			if (arr[x] > arr[maxIndex])
				maxIndex = x;//
		}

		return arr[maxIndex];
	}

	/**
	 * 对数组进行选择排序。
	 * 
	 * @param arr 接收一个元素为int类型的数组。
	 */
	public static void selectSort(int[] arr) {
		for (int x = 0; x < arr.length - 1; x++) {
			for (int y = x + 1; y < arr.length; y++) {
				if (arr[x] > arr[y])
					swap(arr, x, y);
			}
		}
	}

	/*
	 * 用于给数组进行元素的位置置换。
	 * 
	 * @param arr 接收一个元素为int类型的数组。
	 * 
	 * @param a
	 * 
	 * @param b
	 */
	private static void swap(int[] arr, int a, int b) {
		int temp = arr[a];
		arr[a] = arr[b];
		arr[b] = temp;
	}

	/**
	 * 获取指定的元素在指定数组中的索引.
	 * 
	 * @param arr 接收一个元素为int类型的数组。
	 * @param key 要找的元素。
	 * @return 返回该元素第一次出现的位置,如果不存在返回-1.
	 */
	public static int getIndex(int[] arr, int key) {
		for (int x = 0; x < arr.length; x++) {
			if (arr[x] == key)
				return x;
		}
		return -1;
	}

	/**
	 * 将int数组转换成字符串。格式是:[e1,e2,...]
	 * 
	 * @param arr 接收一个元素为int类型的数组。
	 * @return 返回该数组的字符串表现形式。
	 */
	public static String arrayToString(int[] arr) {
		String str = "[";

		for (int x = 0; x < arr.length; x++) {
			if (x != arr.length - 1)
				str = str + arr[x] + ", ";
			else
				str = str + arr[x] + "]";
		}
		return str;
	}
}


数据类型的参数传递:

//基本数据类型参数传递
class Demo {
	public static void main(String[] args) {

		int x = 3;
		show(x);
		System.out.println("x=" + x);
	}

	public static void show(int x) {
		x = 4;
	}
}

// 引用数据类型参数传递
class Demo {
	int x = 3;

	public static void main(String[] args) {
		Demo d = new Demo();
		d.x = 9;
		show(d);
		System.out.println(d.x);
	}

	public static void show(Demo d) {
		d.x = 4;
	}
}

 

封装的例子:

class Person {
	private/* 私有 */int age;

	public void setAge(int a)// setXxx getXxx
	{
		age = a;
	}

	public int getAge() {
		return age;
	}

	/*
	 * public void haha(int a) { if(a>0 && a<130) { age = a; speak(); } else
	 * System.out.println("错误的数据"); }
	 */
	void speak() {
		System.out.println("age=" + age);
	}
}

class PersonDemo {
	public static void main(String[] args) {
		Person p = new Person();
		// p.age = -20;
		p.haha(-20);
		// p.speak();
	}

	public static void selectSort(int[] arr) {
	}

	private static void swap(int[] arr, int a, int b) {
	}
}


静态调用内存图代码:

class Person {
	private String name;
	private int age;
	static String country = "CN";

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

	public void show() {
		System.out.println(Person.country + ":" + this.name + ":" + this.age);
	}

	public static void method() {
		System.out.println(Person.country);
	}
}

class StaticDemo2 {
	public static void main(String[] args) throws Exception {
		Thread.sleep(5000);
		Person.method();

		Person p = new Person("java", 20);
		p.show();
	}
}


“静态什么时候用”的代码:

class Demo {
	int age;
	static int num = 9;

	Demo(int age) {
		this.age = age;
	}

	public static void speak() {
		System.out.println(num);
	}

	public void show() {
		System.out.println(age);
	}

}

class StaticDemo3 {
	public static void main(String[] args) {
		// Demo d = new Demo(30);
		// d.speak();非静态调用 定义的方法没有意义
		Demo.speak();

		// System.out.println("Hello World!");
	}
}


静态代码块例子代码:

class StaticCode {
	static int num;
	static {
		num = 10;
		// num *=3;
		System.out.println("hahahah");
	}

	StaticCode() {
	}

	static void show() {
		System.out.println(num);
	}
}

class Person {
	private String name;

	{// 构造代码块。可以给所有对象进行初始化的。

		System.out.println("constructor code ");
		// cry();
	}

	static {
		System.out.println("static code");
	}

	Person()// 是给对应的对象进行针对性的初始化。
	{
		name = "baby";
		// cry();
	}

	Person(String name) {
		this.name = name;
		// cry();
	}

	public void cry() {
		System.out.println("哇哇");

	}

	public void speak() {
		System.out.println("name:" + name);
	}

	static void show() {
		System.out.println("show run");
	}
}

class StaticCodeDemo {
	static {
		// System.out.println("a");
	}

	public static void main(String[] args) {

		// Person p = null;
		// p.speak();

		// Person.show();
		// Person p1 = new Person();
		// p1.speak();
		// Person p2 = new Person("旺财");
		// p2.speak();
		// new Person();

		// new StaticCode().show();
		// new StaticCode().show();
		// StaticCode.show();
		// System.out.println("b");
	}
}


单例:(后面在学到多线程时会学到线程安全的单例设计模式)

//饿汉式
class Single//类一加载,对象就已经存在了。
{
	private static Single s = new Single();

	private Single(){}

	public static Single getInstance()
	{
		return s;
	}
}


//懒汉式
class Single2//类加载进来,没有对象,只有调用了getInstance方法时,才会创建对象。
			//延迟加载形式。 
{
	private static Single2 s = null;

	private Single2(){}

	public static Single2 getInstance()
	{
		if(s==null)
			s = new Single2();
		return s;
	}
}


class  SingleDemo
{
	public static void main(String[] args) 
	{
		Single s1 = Single.getInstance();
		Single s2 = Single.getInstance();

		System.out.println(s1==s2);
		
//		Single ss = Single.s;

//		Test t1 = new Test();
//		Test t2 = new Test();
		Test t1 = Test.getInstance();
		Test t2 = Test.getInstance();
		t1.setNum(10);
		t2.setNum(20);
		System.out.println(t1.getNum());
		System.out.println(t2.getNum());
	}
}


class Test
{
	private int num;

	private static Test t = new Test();
	private Test(){}
	public static Test getInstance()
	{
		return t;
	}
	public void setNum(int num)
	{
		this.num = num;
	}
	public int getNum()
	{
		return num;
	}

}


对于内存图,初学者只是看图那是看不懂的,还是要辅助毕老师的视频来看。而学过的人也要常看看图,做到每次看时都能在心里想明白是怎样的一个流程,这就ok了。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值