java新手笔记10 构造器

1.摇奖小程序

package com.yfs.javase;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.Random;

public class Demo1 {

	/**
	 * 模拟摇奖
	 */
	public static void main(String[] args) {
		Random ran = new Random();
		int[] a = new int[7];
		
		System.out.println("开始摇奖:");
		//产生奖号
		for (int i = 0; i < a.length; i++) {
			a[i] = ran.nextInt(33) + 1;
			//找重复的数
			for (int j = 0; j < i; j++) {
				if(a[j] == a[i]) {
					System.out.println("第 " + (i + 1) + "位的" + a[j] + "第" + (j + 1) +"数相同") ;
				    i--;//去重复数
				}
			}
		}
		
		
		System.out.println("本次摇奖的结果:");
		//输出奖号
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + "	");
		}
				
	}

}

 2.构造器

package com.yfs.javase;

public class ConstructionDemo {
	public int b = 10;

	/**
	 * 构造方法
	 * 1.如果类没有提供构造方法 
	 * 编译时 系统自动添加无参的构造方法 
	 * 2.构造方法的作用 实例化对象 
	 * 3.类声明构造方法 系统不再提供构造方法
	 * 4.没有返回值类型声明 与类同名
	 * 5.实例化对象的形式由构造方法决定
	 */
	public ConstructionDemo() {
		System.out.println("创建一个对象");
	}
    //构造方法重载
	public ConstructionDemo(String msg) {
		System.out.println("有参数的构造方法   " +  msg);
	}
	
	public ConstructionDemo(double a, int[] b) {
		System.out.println("有2个参数的构造方法   " +  a);
		//执行输出
		for (int i = 0; i < b.length; i++) {
			System.out.println(b[i]);
		}
	}

	// 普通方法
	public void ConstructionDemo() {
		System.out.println("有返回值的方法 。。。是否创建一个对象");
	}

	public static void main(String[] args) {
		ConstructionDemo cs = new ConstructionDemo();// 调用构造方法

	}

	public void test(int a) {
		System.out.println("call test() ....");
	}

}

 3.构造器test

package com.yfs.javase;

public class ConTest {
	
	public static void main(String[] args) {
		ConstructionDemo  csd = new ConstructionDemo();
        csd.test(20);
        //调用有参数的构造方法 实例化对象
        ConstructionDemo  c1 = new ConstructionDemo("大家好!");
        
        
        //ConstructionDemo  c2 = new ConstructionDemo(20);
        ConstructionDemo  c2 = new ConstructionDemo(20, new int[]{1,2,3});
	}

}

 4.Person类

package com.yfs.javase;
//构造方法之间使用this调用
public class Person {
	String name;
	int age;
	char sex;
	boolean married;
	
	public Person() {
		System.out.println("创建Person对象");
		speak ();//调用其他方法
	}

	public Person (String n) {
		//Person();
		//this();//this调用无参数的构造方法
		this("王五", 25, '女', true);//必须是构造方法第一句代码
		System.out.println("call Person(name)...");
	}
	//构造方法中初始化属性
	public Person (String n, int a, char s, boolean m) {
		name = n;//传入参数赋给属性
		age  = a;
		sex = s;
		married = m;
		System.out.println("Person(String n, int a, char s, boolean m)");
	}

	public void speak () {
		System.out.println(name + " 你是人么?");
	}
	
	public void sleep () {
		System.out.println(name + " 睡觉了吗?");
		speak();//调用其他方法
		//Person();//其他方法不能调用构造方法
	}
	
}

 5.Person类测试1

package com.yfs.javase;

public class PersonTest {

	public static void main(String[] args) {
		Person p1 = new Person("张三", 20, '男', false);
		System.out.println(p1.name);
		System.out.println(p1.age);
		System.out.println(p1.sex);
		System.out.println(p1.married);
		p1.speak();
		p1.sleep();
		
		Person p2 = new Person();

	}

}

 6.Person类2

package com.yfs.javase;

public class Person {
	String name;
	int age;
	char sex;
	boolean married;
	
	public Person() {
		System.out.println("创建Person对象");
	}

	public Person (String name) {//参数名与属性同名  name局部变量
		this.name = name;//属性 this代表当前对象
		System.out.println("call Person("+name+")...");
	}
	
	public void speak () {
		System.out.println(this.name + " 你真的是人吗?");
		System.out.println(this);
	}
	
	public void sleep () {
		System.out.println(name + " 睡觉了吗?");
	}
	//销毁对象是调用的方法
	@Override
	protected void finalize() throws Throwable {
		System.out.println(name  + " 对象被销毁...");
	}
}

 7.Person类2测试(垃圾对象回收)

package com.yfs.javase;

public class PersonTest1 {

	public static void main(String[] args) {
	
		Person p1 = new Person("李四");//对象生命从new
		p1.speak();
		
		Person p2 = new Person();
		p2.speak();

		
		Person p3 = null;
		p3 = new Person("张三");
		
		p3 = null;//生命结束
		System.gc();//强制回收
		
		System.out.println("程序执行结束...");
		
	}

}
原文地址:https://www.cnblogs.com/feilongblog/p/4675266.html