学习Java必备的基础知识09,要想学好必须扎实基本功(⭐建议收藏)#yyds干货盘点#
Posted java厂长
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习Java必备的基础知识09,要想学好必须扎实基本功(⭐建议收藏)#yyds干货盘点#相关的知识,希望对你有一定的参考价值。
Day09-Java
@[toc]
关于作者
• 作者介绍
//电脑
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[]){
}
}
重点
数据表与简单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("------------------------------------------------------------------");
}
}
数据表与简单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());
}
}
}
以上是关于学习Java必备的基础知识09,要想学好必须扎实基本功(⭐建议收藏)#yyds干货盘点#的主要内容,如果未能解决你的问题,请参考以下文章
学习Java必备的基础知识打卡12.27,要想学好必须扎实基本功(⭐建议收藏)#yyds干货盘点#
学习Java必备的基础知识打卡12.16,要想学好必须扎实基本功(⭐建议收藏)#yyds干货盘点#
学习Java必备的基础知识06,要想学好必须扎实基本功(⭐建议收藏)#yyds干货盘点#
学习Java必备的基础知识打卡12.19,要想学好必须扎实基本功(⭐建议收藏)#yyds干货盘点#