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
私有化
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);//yyyA.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++;
}
}