欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

面向对象unit02,重载,类和public,构造方法。this,引用类型数组

程序员文章站 2022-05-22 19:20:57
...

面向对象unit02,重载,类和public,构造方法。this,引用类型数组

一,重载

1,方法的重载:
(1)同一个类里面,方法名相同,参数列表不同
(2)编译器在编译的时候自动根据方法的签名来绑定
class Aoo{
void pay(){}
void pay(double d){}
void pay(String s1,String s2){}
void pay(int a,double d){}
void pay(double d,int a){}

void pay(int num,double dou){} //错误
int pay(){} //错误
}

用户:

void println(){}
void printlnString(String str){}
void printlnInt(int n){}
void printlnDouble(double d){}
void printlnBoolean(boolean b){}

System.out.println();
System.out.println(“HelloWorld”);
System.out.println(111);
System.out.println(5.55);

java建议:
1个文件只包含1个类

二,类和public

java规定:
java中一个文件可以包含多个类,
但是,public的类只能有1个,
并且,public的类必须与文件名相同

语法:
1.class—成员变量、方法
2.测试类—main(){创建对象}

三,构造方法:

构造方法同名的有:构造器,构建器,构造函数。
1.构造方法常常用于给成员变量初始化
2.与类同名,没有返回值类型
3.构造方法是在创建对象(new)时被自动调用
4.若自己不写构造方法,
则编译器默认给一个无参构造,
若自己写了有参构造,则不再默认提供无参构造
所以一般同时写有参构造和无参构造。
5.构造方法可以重载

四this:

1.this指代当前对象,谁调这个对象指的就是谁。方法中访问成员变量之前默认都有个this。
2.用法:同一个类里面的方法,调用这个类里面,方法外的变量。可用this,不用也默认有,但是成员变量和方法里面的变量重名则以定要加this。
this.成员变量—访问成员变量
this.方法()-----访问方法
this()--------调构造方法=构造方法

成员变量:类内,方法外的变量。

class Cell{
int row;
int col;
Cell(int row,int col){
this.row = row;
this.col = col;
}
Cell(int n){
this(n,n);
}
Cell(){
this(0,0); //调两个int型参数的构造
}
}

class Cell{
int row;
int col;
Cell(int row,int col){
this.row = row;
this.col = col;
}
void drop(){
row++;
//this.row++; //相当于c.row++
}
}
main(){
Cell cc = new Cell(2,8);
cc.drop();

Cell c = new Cell(5,4);//c.row=5,c.col=4
c.drop();
System.out.println(c.row); //6

Cell c = new Cell(5,4);
Cell cc = new Cell(8,2);
}

class Cell{
int row;
int col;
void drop(){}
void moveLeft(){}
String getCellInfo(){}

Cell(){
}
Cell(int n){
row = n;
col = n;
}
Cell(int row,int col){
this.row = row;
this.col = col;
}
}

class CellTest{
main(){
Cell c = new Cell();
Cell c = new Cell(5);
Cell c = new Cell(5,4);
Cell c = new Cell();
c.row = 5;
c.col = 4;
}
}

class Student{ //学生类
String name;
int age;
String address;
void study(){}

void sayHi(){
study();
this.study();

  System.out.println(name+",,,"+age+",,,"+address);

}

Student zs = new Student(“zhangsan”,25,“河北”);
zs.sayHi();

Student ls = new Student(“lisi”,26,“黑龙江”);
ls.sayHi();

Student(){
}
Student(String name,int age,String address){
this.name = name;
this.age = age;
this.address = address;
//this.name—指的是成员变量
//name--------指的是局部变量/参数
}
}
class StudentTest{
main(){
Student zs = new Student();
Student zs = new Student(“zhangsan”,25,“河北”);

  Student ls = new Student("lisi",26,"黑龙江");

  Student zs = new Student();
  zs.setInfo("zhangsan",25,"河北");

  Student ls = new Student();
  ls.setInfo("lisi",18,"黑龙江");


  Student zs = new Student();
  zs.name = "zhangsan";
  zs.age = 25;
  zs.address = "河北";

  Student ls = new Student();
  ls.name = "lisi";
  ls.age = 23;
  ls.address = "黑龙江";

}
}

明int型数组,名为arr,包含4个元素
arr中的每一个元素都是int类型
int [] arr = new int[4];—基本类型数组

声明Cell型数组,名为arr,包含4个元素
arr中的每一个元素都是Cell类型
Cell [] arr = new Cell[4];—引用类型数组

int是数据类型
Cell是数据类型

int [] arr = new int[4];
boolean[] bs = new boolean[6];
Student[] stus = new Student[100];
Random [] rs = new Random[4];
Scanner[] ss = new Scanner[8];

Hero hero = new Hero(); //英雄机

五,引用类型数组

1.引用类型数组的定义
二维数组在java中叫数组的数组,不叫数组,而且在引用数据类型时候,
不能省一维。
Cell[] cs = new Cell[4];
2.引用类型数组的初始化

  1. Cell[] cells = new Cell[4]; //new数组
    cells[0] = new Cell();
    cells[1] = new Cell(2);
    cells[2] = new Cell(5,4);
    cells[3] = new Cell(2,6);
  2. Cell[] cells = {
    new Cell(),
    new Cell(1),
    new Cell(5,4),
    new Cell(2,7)
    };
  3. Cell[] cells = new Cell[]{
    new Cell(),
    new Cell(1),
    new Cell(5,4),
    new Cell(2,7)
    };

数组也是一种数据类型

int [] arr = new int [4];
Cell [] cs = new Cell[4];

声明int[]类型的数组,名为as,包含4个元素
as为数组的数组,里面每一个元素都是int[]类型
每一个元素,默认值为null
as[0]默认值为null
as[0]是int[]类型

int[] [] as = new int[4][];
as[0] = new int[2];
as[1] = new int[4];
as[2] = new int[2];
as[3] = new int[5];

int[][] as = new int[3][];
as[0] = new int[4];
as[1] = new int[4];
as[2] = new int[4];
简写:
int[][] as = new int[3][4];
for(int i=0;i<3;i++){ //行
for(int j=0;j<4;j++){ //列
as[i][j] = 100;
}
}

相关标签: 02-oop面向对象