Day06-Java
1、this關鍵字
首先需要提醒的是,在整個Java之中,this是最麻煩的一個關鍵字,只要是代碼開發,幾乎都離不開this,在Java中this可以完成三件事情:表示本類屬性、表示本類方法、表示當前物件(只介紹概念),
1.1 “this.屬性”表示本類屬性
在講解著一操作之前首先觀察如下程式:
class Person{
private String name;
private int age;
public Person(String n,int a){
name = n;
age = a;
}
public String getInfo(){
return "姓名:"+name+","+"年齡:"+age;
}
}
public class ThisText{
public static void main(String args[]){
Person per = new Person("張三",20);
System.out.println(per.getInfo());
}
}
這個時候的構造方法的兩額引數的目的是為了類中的name和age兩額屬性初始化,可是這個方法上的兩個引數一個是字母n一個是字母a,什么意思?那么最好的做法,既然構造方法的兩個引數是為了類中name和age初始化使用,那么最好將其引數名稱也定義為name和age才最為直觀,
public Person(String name,int age){
name = name;
age = age;
}
此時構造方法中引數的名稱變得有意義了,但是這樣一來就出現問題了,發現屬性沒有內容了!因為在程式之中是采用“{}”作為分界,采用就近的取用原則,所以現在為了明確指定要操作的是類中屬性的話,那么應該采用“this.屬性”的形式完成,代碼應該變為:
public Person(String name,int age){
this.name = name; //this.name 表示類中的屬性name
this.age = age;
}
提示:在日后的所有開發程序之中,如果要呼叫類中屬性的話,都要使用“this.屬性”的方式來呼叫,
1.2“this”表示呼叫本類方法
對于一個類之中的方法分為兩種:
1、普通方法:之前強調過,如果現在要呼叫的是本類之中的方法,則可以使用“this.方法()”呼叫,
2、構造方法:呼叫其他構造使用“this ()”呼叫,
例如:現在一個類之中存在了三個構造方法(無參、有一個引數、有兩個引數),但是不管使用何種構造方法,都要求在實體化物件產生的時候輸出一行提示資訊:“歡迎光臨”,
class Person{
private String name;
private int age;
public Person(){
System.out.println("-----歡迎光臨-----");
}
public Person(String name){
System.out.println("-----歡迎光臨-----");
this.name = name;
}
public Person(String name,int age){
System.out.println("-----歡迎光臨-----");
this.name = name;
this.age = age;
}
public String getInfo(){
return "姓名:"+name+","+"年齡:"+age;
}
}
public class ThisText1{
public static void main(String args[]){
Person per = new Person();
System.out.println(per.getInfo());
}
}
不過,遺憾的是按照之前的知識來講,此時的程式之中會出現大量的重復代碼,而這樣的方法不是最優的,
這種情況下就可以利用this()來完成
class Person{
private String name;
private int age;
public Person(){
System.out.println("-----歡迎光臨-----");
}
public Person(String name){
this(); //呼叫無參構造方法
this.name = name;
}
public Person(String name,int age){
this(name); //呼叫有一個引數的構造方法
this.age = age;
}
public String getInfo(){
return "姓名:"+name+","+"年齡:"+age;
}
public class ThisText1{
public static void main(String args[]){
Person per = new Person();
System.out.println(per.getInfo());
}
}
使用this()就完成了構造方法之間的互相呼叫,
**注意:**在使用this()呼叫構造方法的時候有以下問題:
1、所有的構造方法實在物件實體化的時候被默認呼叫,而且是在呼叫普通方法之前呼叫,所以使用“this()”呼叫構造方法的操作,一定要放在構造方法的首行;
2、如果一個類之中存在了多種構造方法的話,并且這些構造方法都使用this()相互呼叫,那么至少要保證一個構造方法沒有呼叫其他構造,以作程式的出口,
1.3“this”表示當前物件
class Dx{
public void fun(){
System.out.println("當前物件:" + this);
}
}
public class Ob{
public static void main(String args[]){
Dx dx = new Dx();
System.out.println(dx);
dx.fun();
Dx dx1 = new Dx();
System.out.println(dx1);
dx1.fun();
}
}

1.4參考傳遞進階分析
程式一
class Message{
private String num;
public void setNum(String num){
this.num = num;
}
public String getNum(){
return num;
}
}
public class TestDemo1{
public static void main(String args[]){
Message message = new Message();
message.setNum("100");
fun(message);
//30
System.out.println(message.getNum());
}
public static void fun(Message temp){ //參考傳遞
temp.setNum("30");
}
}

程式二
public class TestDemo2{
public static void main(String args[]){
String str = "hello";
fun(str);
//hello
System.out.println(str);
}
public static void fun(String temp){
temp = "world";
}
}
本程式的關鍵是:String的內容一旦宣告則不可改變,改變的是記憶體的地址指向,

1.5物件比較
物件的比較實際就是物件屬性的比較,
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
}
public class TestDemo3{
public static void main(String args[]){
Person per1 = new Person("張三",20);
Person per2 = new Person("張三",20);
//false
System.out.println(per1==per2); //比較兩個物件的地址
//是同一個物件
if(per1.getName().equals(per2.getName())&&per1.getAge() == per2.getAge()){
System.out.println("是同一個物件");
}
else{
System.out.println("不是同一個物件");
}
}
}
物件比較的操作一定是一個類自己本身所具備的功能,而且物件比較的操作特點:
1 本類接收自己的參考,而后與本類當前物件(this)進行比較;
2 為了避免NullPointerException的產生,應該增加一個null的判斷;
3 為了防止浪費性能的情況出現,可以增加地址數值的判斷,相同的物件地址相同;
4之后與屬性依次進行比較,如果屬性全部相同,則回傳true,否則回傳false,
1.6參考傳遞實際應用
//電腦
class Computer{
private View [];
private Host;
}
//顯示幕
class View{
}
//主機
class Host{
private Board;
}
//主板
class Board{
private Cpu [];
private Memory[];
private Disk[];
}
//CPU
class Cpu{
}
//記憶體
class Memory{
}
//硬碟
class Disk{
}
public class TestDemo3{
public static void main(String args[]){
}
}
重點
1.7資料表與簡單Java類(一對多)
利用此關系模型,表示出emp和dept的關系,使用欄位:
? **emp表:**empno、ename、job、sal、comm、mgr、deptno;
? **dept表:**deptno、dname、loc,
第一步 撰寫物體類
class Emp{
private int empno;
private String ename;
private String job;
private double sal;
private double comm;
public Emp(int empno,String ename,String job,double sal,double comm){
this.empno = empno;
this.ename = ename;
this.job = job;
this.sal = sal;
this.comm = comm;
}
public String getEmpInfo(){
return "【Emp】 empno = " + this.empno +
",ename = " + this.ename +
",job; = " + this.job +
",sal = " + this.sal +
",comm = " + this.comm;
}
}
class Dept{
private int deptno;
private String dname;
private String loc;
public Dept(int deptno, String dname, String loc){
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public String getDept(){
return "deptno = " + this.deptno +
",dname = " + this.dname +
",loc = " + this.loc;
}
}
public class Exam1_7{
public static void main(String args[]){
}
}
第二步 進行關系設計
一個雇員屬于一個部門,需要追加部門參考
一個雇員有一個領導,領導一定是自身關聯
一個部門有一個雇員,需要一個物件陣列來描述多個雇員資訊
class Emp{
private int empno;
private String ename;
private String job;
private double sal;
private double comm;
private Emp mgr; //描述雇員的領導
private Dept dept; //描述雇員的部門
public Emp(int empno,String ename,String job,double sal,double comm){
this.empno = empno;
this.ename = ename;
this.job = job;
this.sal = sal;
this.comm = comm;
}
public void setMgr(Emp mgr){
this.mgr = mgr;
}
public Emp getMgr(){
return this.mgr;
}
public void setDept(Dept dept){
this.dept = dept;
}
public Dept getDept(){
return this.dept;
}
public String getEmpInfo(){
return "【Emp】 empno = " + this.empno +
",ename = " + this.ename +
",job; = " + this.job +
",sal = " + this.sal +
",comm = " + this.comm;
}
}
class Dept{
private int deptno;
private String dname;
private String loc;
private Emp [] emps; //一個部門有多個雇員
public Dept(int deptno, String dname, String loc){
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public void setEmps(Emp [] emps){
this.emps = emps;
}
public Emp [] getEmps(){
return this.emps;
}
public String getDept(){
return "deptno = " + this.deptno +
",dname = " + this.dname +
",loc = " + this.loc;
}
}
public class Exam1_7{
public static void main(String args[]){
}
}
此時基本類定義完成,
第三步 開發需求
主函式main
public class Exam1_7{
public static void main(String args[]){
//第一步、設定類物件的關系
//1.分別創建各自物件實體化
Dept dept = new Dept(10,"市場部","New York");
Emp ea = new Emp(7345,"Rock","CLERK",800.0,0);
Emp eb = new Emp(7567,"Joker","MANAGER",3050.0,0);
Emp ec = new Emp(7825,"Ben","PRESIDENT",10000.0,0);
//2.設定雇員和領導的關系
ea.setMgr(eb);
eb.setMgr(ec); //ec沒有領導 自己就是最大的領導
//3.設定雇員和部門的關系
ea.setDept(dept);
eb.setDept(dept);
ec.setDept(dept);
//4.設定部門的雇員
dept.setEmps(new Emp[]{ea, eb, ec});
//第二步、進行資料的取得
//一個部門有多個雇員
/*
【Emp】 empno = 7345,ename = Rock,job = CLERK,sal = 800.0,comm = 0.0
【Emp】 empno = 7567,ename = Joker,job = MANAGER,sal = 3050.0,comm = 0.0
【Emp】 empno = 7825,ename = Ben,job = PRESIDENT,sal = 10000.0,comm = 0.0
*/
for(int i = 0; i < dept.getEmps().length; i++){
System.out.println(dept.getEmps()[i].getEmpInfo());
}
System.out.println();
//一個雇員有一個領導
//【Emp】 empno = 7567,ename = Joker,job = MANAGER,sal = 3050.0,comm = 0.0
System.out.println(ea.getMgr().getEmpInfo());
//【Emp】 empno = 7825,ename = Ben,job = PRESIDENT,sal = 10000.0,comm = 0.0
System.out.println(eb.getMgr().getEmpInfo());
//Exception in thread "main" java.lang.NullPointerException
//沒有設定關系 所以自己是老板 顯示空指標例外
//System.out.println(ec.getMgr().getEmpInfo());
System.out.println();
//一個雇員屬于同個部門
//deptno = 10,dname = 市場部,loc = New York
System.out.println(ea.getDept().getDeptInfo());
//deptno = 10,dname = 市場部,loc = New York
System.out.println(eb.getDept().getDeptInfo());
//deptno = 10,dname = 市場部,loc = New York
System.out.println(ec.getDept().getDeptInfo());
}
}
在上面的代碼基礎上進行改進
//一個部門有多個雇員,一個雇員有一個領導,一個雇員屬于同個部門
/*
【Emp】 empno = 7345,ename = Rock,job = CLERK,sal = 800.0,comm = 0.0
該雇員所屬的領導【Emp】 empno = 7567,ename = Joker,job = MANAGER,sal = 3050.0,comm = 0.0
該雇員所屬的部門deptno = 10,dname = 市場部,loc = New York
------------------------------------------------------------------
【Emp】 empno = 7567,ename = Joker,job = MANAGER,sal = 3050.0,comm = 0.0
該雇員所屬的領導【Emp】 empno = 7825,ename = Ben,job = PRESIDENT,sal = 10000.0,comm = 0.0
該雇員所屬的部門deptno = 10,dname = 市場部,loc = New York
------------------------------------------------------------------
【Emp】 empno = 7825,ename = Ben,job = PRESIDENT,sal = 10000.0,comm = 0.0
自己就是領導
------------------------------------------------------------------
*/
for(int i = 0; i < dept.getEmps().length; i++){
System.out.println(dept.getEmps()[i].getEmpInfo());
if(dept.getEmps()[i].getMgr() != null){
System.out.println("該雇員所屬的領導" +
dept.getEmps()[i].getMgr().getEmpInfo());
System.out.println("該雇員所屬的部門" +
dept.getEmps()[i].getDept().getDeptInfo());
System.out.println("------------------------------------------------------------------");
}
else{
System.out.println("自己就是領導");
System.out.println("------------------------------------------------------------------");
}
}
1.8資料表與簡單Java類(多對多)
利用此關系模型,表示出student和course的關系,使用欄位:
? **student表:**stuid、name、age,
? **course表:**cid、name、credit,
? 關系表:學生編號、課程編號、成績
要求:
可以找到一門課程,參加此次課程的所有學生資訊和成績
可以根據一個學生,找到所參加的所有課程和沒門課程的一個成績
第一步 撰寫物體類
class Student{
private int stuid;
private String name;
private int age;
public Student(int stuid, String name, int age){
this.stuid = stuid;
this.name = name;
this.age = age;
}
public String getStuInfo(){
return "【Student】stuid = " + this.stuid +
",name = " + this.name +
",age = " +this.age ;
}
}
class Course{
private int cid;
private String name;
private double credit;
public Course(int cid, String name, double credit){
this.cid = cid;
this.name = name;
this.credit = credit;
}
public String getCouInfo(){
return "【Course】cid = " + this.cid +
",name = " + this.name +
",credit = " + this.credit;
}
}
class StudentCourse{//學生選課
private Student student;
private Course course;
private double score;
public StudentCourse(Student student, Course course, double score){
this.student =student;
this.course = course;
this.score = score;
}
public Student getStudent(){
return this.student;
}
public Course getCrouse(){
return this.course;
}
public double getScore(){
return this.score;
}
}
第二步 進行關系設計
class Student{
private int stuid;
private String name;
private int age;
private StudentCourse studentCourses [];
public Student(int stuid, String name, int age){
this.stuid = stuid;
this.name = name;
this.age = age;
}
public void setStudentCourses(StudentCourse []studentCourses){
this.studentCourses = studentCourses;
}
public StudentCourse[] getStudentCourses(){
return this.studentCourses;
}
public String getStuInfo(){
return "【Student】stuid = " + this.stuid +
",name = " + this.name +
",age = " +this.age ;
}
}
class Course{
private int cid;
private String name;
private double credit;
private StudentCourse studentCourses[];
public Course(int cid, String name, double credit){
this.cid = cid;
this.name = name;
this.credit = credit;
}
public void setStudentCourses(StudentCourse []studentCourses){
this.studentCourses = studentCourses;
}
public StudentCourse[] getstudentCourses(){
return this.studentCourses;
}
public String getCouInfo(){
return "【Course】cid = " + this.cid +
",name = " + this.name +
",credit = " + this.credit;
}
}
class StudentCourse{//學生選課
private Student student;
private Course course;
private double score;
public StudentCourse(Student student, Course course, double score){
this.student =student;
this.course = course;
this.score = score;
}
public Student getStudent(){
return this.student;
}
public Course getCrouse(){
return this.course;
}
public double getScore(){
return this.score;
}
}
第三步 開發需求
主函式main
public class Exam1_8{
public static void main(String args[]){
//第一步、設定類物件的關系
//1.分別創建各自物件實體化
Student stu1 = new Student(107,"Rock",13);
Student stu2 = new Student(108,"Joker",18);
Student stu3 = new Student(109,"Perke",22);
Course ca = new Course(1,"資料結構",4.0);
Course cb = new Course(2,"計算機作業系統",2.0);
Course cc = new Course(3,"SSM框架集合",3.0);
//2.設定學生和課程的關系
stu1.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu1,ca,78.0),
});
stu2.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu2,ca,87.0),
new StudentCourse(stu2,cb,79.0)
});
stu3.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu3,ca,90.0),
new StudentCourse(stu3,cb,95.0),
new StudentCourse(stu3,cc,99.0)
});
//3.設定課程和學生的關系
ca.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu1,ca,78.0),
new StudentCourse(stu2,ca,87.0),
new StudentCourse(stu3,ca,90.0)
});
cb.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu2,cb,79.0),
new StudentCourse(stu3,ca,90.0)
});
cc.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu3,cc,99.0)
});
//第二步、進行資料的取得
//可以找到一門課程,參加此次課程的所有學生資訊和成績
/*
【Course】cid = 1,name = 資料結構,credit = 4.0
學生名單:【Student】stuid = 107,name = Rock,age = 13,成績:78.0
學生名單:【Student】stuid = 108,name = Joker,age = 18,成績:87.0
學生名單:【Student】stuid = 109,name = Perke,age = 22,成績:90.0
*/
System.out.println(ca.getCouInfo());
for(int i = 0 ;i < ca.getStudentCourses().length ; i++){
System.out.print("學生名單:"+ ca.getStudentCourses()[i].getStudent().getStuInfo());
System.out.println(",score = "+ ca.getStudentCourses()[i].getScore());
}
System.out.println("--------------------------------------------------------------------");
//可以根據一個學生,找到所參加的所有課程和沒門課程的一個成績
/*
【Student】stuid = 109,name = Perke,age = 22
選課串列:【Course】cid = 1,name = 資料結構,credit = 4.0,score = 90.0
選課串列:【Course】cid = 2,name = 計算機作業系統,credit = 2.0,score = 95.0
選課串列:【Course】cid = 3,name = SSM框架集合,credit = 3.0,score = 99.0
*/
System.out.println(stu3.getStuInfo());
for(int i = 0; i < stu3.getStudentCourses().length ; i++){
System.out.print("選課串列:" + stu3.getStudentCourses()[i].getCourse().getCouInfo());
System.out.println(",score = "+ stu3.getStudentCourses()[i].getScore());
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/291631.html
標籤:java
