构造方法及其类加载面试题

Day09

一、构造方法

1.含义

与类名相同,且没有返回项的方法

2.作用

1.和new在一起是创建对象
2.初始化属性

3.注意

1.该类没有有参构造时,无参构造(没有参数的构造方法)系统会默认实现
2.构造方法可以重载

public class Person {
	
	String name;
	char sex;
	int age;
	
	public Person(){}
	
	public Person(String name,char sex,int age){
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	
	public void eat() {
		System.out.println(this.name + "吃饭饭");
	}
	
	public void sleep() {
		System.out.println(this.name + "睡觉觉");
	}

}

public class Test01 {
	
	public static void main(String[] args) {
		Person p = new Person("林成", '男', 18);
		p.eat();
		
	}

}

二、五子棋

public class GoBang {

	private int length = 20;//棋盘长度
	private String[][] goBang = new String[length][length];//棋盘的容器
	//棋盘符号
	private String add = "╋";
	private String black = "■";
	private String white = "○";
	private String[] nums = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};

	public GoBang() {
		init();
		printGoBang();
	}
	
	//初始化棋盘
	private void init(){
		for (int i = 0; i < goBang.length; i++) {
			for (int j = 0; j < goBang[i].length; j++) {
				if(j == length-1){
					goBang[i][j] = nums[i];
				}else if(i == length-1){
					goBang[i][j] = nums[j];
				}else{
					goBang[i][j] = add;
				}
			}
		}
	}

	//打印棋盘
	public void printGoBang(){
		for (String[] strings : goBang) {
			for (String string : strings) {
				System.out.print(string);
			}
			System.out.println();
		}
	}

	//判断坐标是否超出棋盘范围
	private boolean isIndexOutOfGoBang(int x,int y){
		if(x < 0 || x > length-2 || y < 0 || y > length-2){
			return false;
		}
		return true;
	}

	//判断坐标上是否有棋子
	private boolean isGoBang(int x,int y){
		if(!goBang[x][y].equals(add)){
			return false;
		}
		return true;
	}

	/**
	 * 落子
	 * @param x x坐标
	 * @param y y坐标
	 * @param flag true-黑子 false-白子
	 * @return -1:落子失败 - 坐标超出棋盘范围
	 * 		   -2:落子失败 - 坐标上有棋子
	 * 			1:落子成功
	 */
	public int play(int x,int y,boolean flag){
		
		if(!isIndexOutOfGoBang(x, y)){
			return -1;
		}
		
		if(!isGoBang(x, y)){
			return -2;
		}
		
		goBang[x][y] = (flag)?black:white;
		return 1;
	}

}
import java.util.Scanner;

public class Test01 {

	public static void main(String[] args) {
		
		GoBang goBang = new GoBang();
		
		Scanner scan = new Scanner(System.in);
		boolean flag = true;//true-黑子 false-白子
		while(true){
			
			System.out.println("请" + ((flag)?"黑":"白") + "子输入坐标:");
			int x = scan.nextInt()-1;
			int y = scan.nextInt()-1;
			
			//落子
			int play = goBang.play(x, y, flag);
			if(play == -1){
				System.out.println("坐标超出棋盘范围,请重新输入...");
				continue;
			}else if(play == -2){
				System.out.println("坐标上有棋子,请重新输入...");
				continue;
			}
			
			//置反
			flag = !flag;
			
			//打印棋盘
			goBang.printGoBang();
			
		}
	}
}

三、private

  1. 含义

私有化

2.作用

1.修饰属性:私有化属性,不能让外界调用
2.修饰方法:私有化方法,不能让外界调用

3.应用场景

不让外界调用的属性就使用private修饰
不让外界调用的方法就使用private修饰

public class A {
	
	private String str;
	
	private void method01(){
		this.str = "aaa";
		System.out.println(this.str);
	}
	
	public void method02(){
		this.method01();
	}
}

public class Test01 {

	public static void main(String[] args) {
		A a = new A();
		
		a.method02();
	}
}

四、this - 本对象

1.含义

方法中this表示调用方法的对象

2.作用

1.this.成员变量:调用本对象的成员变量
2.this.成员方法:利用本对象调用成员方法
3.this():调用本类的构造方法
注意:在一个构造方法中的第一句调用另外一个构造方法

public class Person {

	private String name;
	private char sex;
	private int age;
	
	public Person() {
		//调用本类的构造方法
		this("默认姓名", '男', 18);
	}
	
	public Person(String name, char sex) {
		this.name = name;
		this.sex = sex;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public void eat(){
		//this.name:调用本对象的成员变量
		System.out.println(this.name + "吃饭饭");
		//利用本对象调用成员方法
		this.sleep();
	}
	
	public void sleep(){
		System.out.println(this.name + "睡觉觉");
	}
	
	
}

public class Test01 {
	
	public static void main(String[] args) {
		
		Person p = new Person();
		
		System.out.println(p.getName());
		
		p.eat();
	}

}

五、static修饰属性

1.含义

静态的

成员变量每个对象独享一份
静态变量每个对象共享一份

2.静态变量的生命周期

字节码文件加载到方法区时,系统会扫描该类所有的属性,并把静态变量存放到静态区
项目结束时,静态变量才会被回收

public class A {
	
	//成员变量
	String str1;
	
	//静态变量
	static String str2 ;
}

public class Test01 {
public static void main(String[] args) {
	A a1 = new A();
	A a2 = new A();

	a1.str1 = "aaa";
	a2.str1 = "bbb";
	System.out.println(a1.str1);//aaa
	System.out.println(a2.str1);//bbb

// a1.str2 = “xxx”;
// a2.str2 = “yyy”;
// System.out.println(a1.str2);//yyy
// System.out.println(a2.str2);//yyy

	A.str2 = "xxx";
	A.str2 = "yyy";
	System.out.println(A.str2);//yyy
	System.out.println(A.str2);//yyy
}

}

六、类加载机制面试题

1.
public class Test01 {

	public static void main(String[] args) {
		/**
		 * 知识点:类加载机制面试题 
		 */

		System.out.println("A value1:" + A.value1);//1
		System.out.println("A value2:" + A.value2);//0
	}
}

class A{
	
	/**
	 * 准备阶段1:
	 * 	A a;
	 *  int value1;
	 *  int value2;
	 *  
	 * 准备阶段2:
	 * 	A a = null;
	 *  int value1 = 0;
	 *  int value2 = 0;
	 *  
	 * 初始化阶段:
	 * 	A a = new A();
	 *  int value1 = 1;
	 *  int value2 = 0;
	 */
	
	private static A a = new A();
	public static int value1;	
	public static int value2 = 0;

	private A(){
		value1++;
		value2++;
	}

}

2.
public class Test01 {

	public static void main(String[] args) {
		/**
		 * 知识点:类加载机制面试题 
		 */
		System.out.println("B value1:" + B.value1);//1
        System.out.println("B value2:" + B.value2);//1
	}
}

class B{
	
	/**
	 * 准备阶段1:
	 * 	int value1;
	 * 	int value2;
	 * 	B b;
	 * 
	 * 准备阶段2:
	 * 	int value1 = 0;
	 * 	int value2 = 0;
	 * 	B b = null;
	 * 
	 * 初始化阶段:
	 * 	int value1 = 1;
	 * 	int value2 = 1;
	 * 	B b = new B();
	 */
    public static int value1;
    public static int value2 = 0;
    private static B b = new B();

    private B(){
        value1++;
        value2++;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呆呆傻傻代码搬运工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值