关于java的学习记录:
1.
/**
* 新建一个雇员类,里面包含有雇员编号、姓名、职位、基本工资、佣金
* 这中功能的类在开发中被称为简单java类,因为这些类里面不会有过于复杂的
* 程序逻辑
* 对于简单java类而言,那么现在可以给出它的第一种开发形式:
* 1.类名称必须存在有意义,例如:Book、Emp
* 2.类之中 的属性必须private封装,封装后的属性必须提供setter、getter
* 3.类中可以提供有任意多个构造方法,但是必须保留有一个无参构造方法
* 4.类之中不允许出现任何的输出语句,所有信息输出必须交给被调用处
* 类之中需要提供有一个取得对象完整信息的方法,暂定为getInfo(),而且返回String型输出
* !!所有类中提供的setter、getter方法可能某些操作不会使用到,但是依然
* 必须提供,所有的setter方法除了具备设置属性的内容外,还具有修改的功能
*
* 简单java类是日后进行整个项目开发的千分之一组成部分,也是最为重要的组成部分
*/
public class Demo1 {
public static void main(String[] args) {
Emp1 e=new Emp1(7369,"haha","clerk",800.0,300.0);
e.setEname("kaka");
System.out.println(e.getInfo());
}
}
class Emp1{ //定义一个有意义的类
private int empno;
private String ename;
private String job;
private double sal;
private double comm;
public void setEmpno(int empno) {
this.empno=empno;
}
public int getEmpno() {
return empno;
}
public void setEname(String ename) {
this.ename=ename;
}
public String getEname() {
return ename;
}
public void setJob(String job) {
this.job=job;
}
public String getJob() {
return job;
}
public void setSal(double sal) {
this.sal=sal;
}
public double getSal() {
return sal;
}
public void setComm(double comm) {
this.comm=comm;
}
public double getComm() {
return comm;
}
public Emp1() {}
public Emp1(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 getInfo() {
return "雇员编号:"+empno+"\n"+
"雇员姓名:"+ename+"\n"+
"雇员职位:"+job+"\n"+
"基本工资:"+sal+"\n"+
"佣 金:"+comm;
}
}
2.
/**
* 简单java类一定要和数据表的结构对应上,同时外键需要依靠引用来进行关联,于是
* 本次将针对简单java类做一个功能上的扩充
* 要求通过java程序描述出dept-emp关系,是用字段:
* dept:deptno/dname/loc
* emp:empno/ename/job/sal/comm/deptno/mgr
* 在dept-emp表的关系里存在有如下关联:
* 一个部门有多个雇员、一个雇员有1个或0个领导
* 第一步:实现基本字段的转换
* 第二步:解决外键关系
* 一个雇员属于一个部门,那么应该在雇员里面保存部门信息,修改Emp类
* 一个部门有多个雇员,如果要描述多这个概念应该使用数组完成,修改Dept类
* 一个雇员有一个领导
* 所有的映射关系都已经可以成功的描述出来了
* 第三步:设置并取得数据
* 对于这种映射的方式一定要分两步完成,第一步根据结构设置数据,第二步根据结构取出数据
* 设置和取出一般不会放一起
* 第一步:设置数据
* 第二步:取出数据,根据给定结构取出数据,要求如下:
* 可以根据一个雇员查询它所对应的领导信息和部门信息
* 可以根据一个部门取出所有的雇员以及每个雇员的领导信息
*/
public class Demo2 {
public static void main(String[] args) {
//第一步:设置数据
//1.产生各自的独立对象
Dept dept=new Dept(10,"ACCOUNTING","NewYork");//部门信息
Emp ea=new Emp(7369,"SMTTH","CLERK",800.0,0.00);//雇员信息
Emp eb=new Emp(7902,"FORD","MANAGER",2450.0,0.00);//雇员信息
Emp ec=new Emp(7369,"KING","PRISIDENT",5000.0,0.00);//雇员信息
//2.设置雇员和领导关系
ea.setMgr(eb);
eb.setMgr(ec);
//3.设置雇员和部门关系
ea.setDept(dept); //雇员与部门关系
ea.setDept(dept);
ea.setDept(dept);
dept.setEmps(new Emp[] {ea,eb,ec});
//第二步:取出数据
//1.通过雇员找领导信息和部门信息
System.out.println(ea.getInfo());
System.out.println("\t领导"+ea.getMgr().getInfo());
System.out.println("\t部门"+ea.getDept().getInfo());
//2.根据部门找到所有的雇员以及每个雇员的领导信息
System.out.println("---------------------------------");
System.out.println(dept.getInfo());
for(int x=0;x<dept.getEmps().length;x++) {
System.out.println("\t"+dept.getEmps()[x].getInfo());
if(dept.getEmps()[x].getMgr()!=null) {
System.out.println("\t\t"+dept.getEmps()[x].getMgr().getInfo());
}
}
//整个代码之中都是依靠代码链进行数据取出的
/**合格掌握:
* 可以将任意给出的数据表直接转换为类的形式,并设置或
* 取出所有的数据
*/
}
}
class Dept{
private int deptno;
private String dname;
private String loc;
private Emp[] emps;//多个雇员
public Dept() {}
public Dept(int deptno,String dname,String loc) {
this.deptno=deptno;
this.dname=dname;
this.loc=loc;
}
public String getInfo() {
return "部门编号:"+deptno+",部门名称:"+dname+",部门地址:"+loc;
}
public void setDeptno(int deptno) {
this.deptno=deptno;
}
public int getDeptno() {
return deptno;
}
public void setDname(String dname) {
this.dname=dname;
}
public String getDname() {
return dname;
}
public void setLoc(String loc) {
this.loc=loc;
}
public String getLoc() {
return loc;
}
public void setEmps(Emp[] emps) {
this.emps=emps;
}
public Emp[] getEmps() {
return emps;
}
}
class Emp{
private int empno;
private String ename;
private String job;
private double sal;
private double comm;
private Dept dept;//表示对应的部门信息
private Emp mgr; //表示雇员对应的领导
public Emp() {}
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 getInfo() {
return "雇员编号:"+empno+",雇员姓名:"+ename+",雇员职位:"+job+",基本工资:"+sal+",佣金:"+comm;
}
public void setEmpno(int empno) {
this.empno=empno;
}
public int getEmpno() {
return empno;
}
public void setEname(String ename) {
this.ename=ename;
}
public String getEname() {
return ename;
}
public void setJob(String job) {
this.job=job;
}
public String getJob() {
return job;
}
public void setSal(double sal) {
this.sal=sal;
}
public double getSal() {
return sal;
}
public void setComm(double comm) {
this.comm=comm;
}
public double getComm() {
return comm;
}
public void setDept(Dept dept) {
this.dept=dept;
}
public Dept getDept() {
return dept;
}
public void setMgr(Emp mgr) {
this.mgr=mgr;
}
public Emp getMgr() {
return mgr;
}
}
3.
/**
* 第一个面向对象学习之中就是以数据表和简单java类的转换操作为终点,也是下一个起点
* 现阶段必须掌握的转换:一对多关系、多对多转换
* 通过3个题目完成
* 题目一:一对多
* 1.要求可以通过一个城市可以找到它所对应的省份信息
* 2.可以通过一个省份找到所有城市的信息
* 第一步:先写出基本字段
* 第二步:设置关联字段(引用)
* 第三步:进行测试
* 按照两步完成,第一步是设置数据,第二步是根据结构取出数据
*/
public class Demo4 {
public static void main(String[] args) {
//1.设置关系数据
//先准备各自独立的对象
Province pro=new Province(1,"河北省");
City c1=new City(1001,"唐山");
City c2=new City(1002,"秦皇岛");
City c3=new City(1002,"石家庄");
//设置关系
c1.setProvince(pro);//一个城市属于一个省份
c2.setProvince(pro);
c2.setProvince(pro);
pro.setCities(new City[] {c1,c2,c3});
//2.取出关系数据
System.out.println(c2.getProvince().getInfo());
for(int i=0;i<pro.getCities().length;i++) {
System.out.println("\t |- "+pro.getCities()[i].getInfo());
}
}
/**
* 功能存在哪些不足?
* 1.增加了几个城市,那么就一定更要修改数组的引用关系
* 2.如果现在删除了两个城市信息,后面又增加了2个城市信息
* 3.要查询某个城市的信息是否存在,遍历数组
* 4.要查询某一个城市的数组保存索引号,遍历数组
* 5.替换掉某一个城市的内容
* 造成所有困难的关键就在于数组的使用上,但又不能不使用数组
*/
}
class Province{//类名称就是表名称
private int pid;
private String name;
private City[] cities;
public Province() {}
public Province(int pid,String name) {
this.pid=pid;
this.name=name;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setCities(City[] cities) {
this.cities=cities;
}
public City[] getCities() {
return cities;
}
public String getInfo() {
return "省份编号:"+this.pid+",省份名称:"+this.name;
}
}
class City{
private int cid;
private String name;
private Province province;
public City() {}
public City(int cid,String name) {
this.cid=cid;
this.name=name;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setProvince(Province province) {
this.province=province;
}
public Province getProvince() {
return province;
}
public String getInfo() {
return "城市编号:"+this.cid+",城市名称:"+this.name;
}
}
4.
/**
* 题目二:一对多
* 类型:类型id 名称 描述
* 子类型:子类型id 类型id 名称 描述
* 商品: 商品id 类型id 子类型id 名称 价格
* 1.可以通过一个类型找到它所对应的全部子类型
* 2.可以通过一个类型找到它所对应的全部商品,以及每个商品对应的子类型
* 3.可以通过一个子类型找到所有对应的全部商品
* 不管日后开发有多么发咋的表关系,按照步骤设计
* 第一步:设置基本字段
* 第二步:设置关系
* 第三步:设置数据和取出数据
*/
public class Demo5 {
public static void main(String[] args) {
//设置数据关系
//在开发中设置关系的时候,中立的做法永远是根据类的结构将完整的数据都取出来并配置好
//引用关系
Item item=new Item(1,"厨房用具","-");
Subitem suba=new Subitem(1001,"厨具","-");
Subitem subb=new Subitem(1002,"刀具","-");
Subitem subc=new Subitem(1003,"餐具","-");
Product proa=new Product(90001,"蒸锅",500);
Product prob=new Product(90002,"炒锅",3000);
Product proc=new Product(90003,"菜刀",1000);
Product prod=new Product(90004,"水果刀",80);
Product proe=new Product(90005,"青花瓷",10000);
Product prof=new Product(90006,"水晶筷子",8000);
//设置引用关系
suba.setItem(item);
subb.setItem(item);
subc.setItem(item);
item.setSubitems(new Subitem[] {suba,subb,subc});
proa.setSubitem(suba);
proa.setItem(item);
prob.setSubitem(suba);
prob.setItem(item);
proc.setSubitem(subb);
proc.setItem(item);
prod.setSubitem(subb);
prod.setItem(item);
proe.setSubitem(subc);
proe.setItem(item);
prof.setSubitem(subc);
prof.setItem(item);
suba.setProducts(new Product[] {proa,prob});
subb.setProducts(new Product[] {proc,prod});
subc.setProducts(new Product[] {proe,prof});
item.setProducts(new Product[] {proa,prob,proc,prod,proe,prof});
//取出数据
//1通过一个类型找到它所对应的全部子类型
System.out.println(item.getInfo());
for(int i=0;i<item.getSubitems().length;i++) {
System.out.println("\t|-"+item.getSubitems()[i].getInfo());
}
//2通过一个类型找到它所对应的全部商品,以及每个商品对应的子类型
System.out.println("--------------------------------");
System.out.println(item.getInfo());
for(int i=0;i<item.getProducts().length;i++) {
System.out.println("\t|-"+item.getProducts()[i].getInfo());
System.out.println("\t\t|-"+item.getProducts()[i].getSubitem().getInfo());
}
//3通过一个子类型找到所有对应的全部商品
System.out.println("----------------------------------");
System.out.println(subb.getInfo());
for(int i=0;i<subb.getProducts().length;i++) {
System.out.println("\t|-"+subb.getProducts()[i].getInfo());
}
}
}
class Item{//父栏目
private int iid;
private String name;
private String note;
private Subitem[] subitems;
private Product[] products;
public Item() {}
public Item(int iid,String name,String note) {
this.iid=iid;
this.name=name;
this.note=note;
}
public int getIid() {
return iid;
}
public void setIid(int iid) {
this.iid = iid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public void setSubitems(Subitem[] subitems) {
this.subitems=subitems;
}
public Subitem[] getSubitems() {
return subitems;
}
public void setProducts(Product[] products) {
this.products=products;
}
public Product[] getProducts() {
return products;
}
public String getInfo() {
return "栏目编号:"+this.iid+",名称:"+this.name+",描述:"+this.note;
}
}
class Subitem{//子栏目
private int sid;
private String name;
private String note;
private Item item;
private Product[] products;
public Subitem() {}
public Subitem(int sid,String name,String note) {
this.sid=sid;
this.name=name;
this.note=note;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public void setItem(Item item) {
this.item=item;
}
public Item getItem() {
return item;
}
public void setProducts(Product[] products) {
this.products=products;
}
public Product[] getProducts() {
return products;
}
public String getInfo() {
return "子栏目编号:"+this.sid+",名称:"+this.name+",描述:"+this.note;
}
}
class Product{//商品
private int pid;
private String name;
private double price;
private Item item;
private Subitem subitem;
public Product() {}
public Product(int pid,String name,double price) {
this.pid=pid;
this.name=name;
this.price=price;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void setItem(Item item) {
this.item=item;
}
public Item getItem() {
return item;
}
public void setSubitem(Subitem subitem) {
this.subitem=subitem;
}
public Subitem getSubitem() {
return subitem;
}
public String getInfo() {
return "商品编号:"+this.pid+",名称:"+this.name+",价格:"+this.price;
}
}
5.
/**
*题目三:多对多
*1.可以根据一个管理员找到它所对应的角色,以及每个角色包含的所有权限组
*的信息,以及每个权限组所包含的所有权限的内容
*2.可以根据一个权限组找到所具备次权限组的角色以及每个角色所拥有的管理员信息
*在本次设计之中给出的角色_权限组表实际上属于一张关系表,他保存的只是两个外键的关联关系
*这样的关系表,不需要为其生成映射类
*生成的只能是实体表的转换操作,而多对多的中间转换只需要通过类属性的关系配置即可
*/
public class Demo6 {
public static void main(String[] args) {
//设置完整关系
//1.准备出若干对象
Admin a1=new Admin("admin","hello");
Admin a2=new Admin("mldn","hello");
Admin a3=new Admin("ayou","hello");
Role r1=new Role(1,"系统管理员");
Role r2=new Role(2,"信息管理员");
Group g1=new Group(10,"信息管理");
Group g2=new Group(11,"用户管理");
Group g3=new Group(12,"数据管理");
Group g4=new Group(13,"接口管理");
Group g5=new Group(14,"备份管理");
Action ac1=new Action(1001,"新闻发布","--");
Action ac2=new Action(1002,"新闻列表","--");
Action ac3=new Action(1003,"新闻审核","--");
Action ac4=new Action(1004,"增加用户","--");
Action ac5=new Action(1005,"用户列表","--");
Action ac6=new Action(1006,"登陆日志","--");
Action ac7=new Action(1007,"雇员数据","--");
Action ac8=new Action(1008,"部门数据","--");
Action ac9=new Action(1009,"公司数据","--");
Action ac10=new Action(1010,"服务传输","--");
Action ac11=new Action(1011,"短信平台","--");
Action ac12=new Action(1012,"全部备份","--");
Action ac13=new Action(1013,"局部备份","--");
//设置基本关系
//1.管理员和角色
a1.setRole(r1);
a2.setRole(r2);
a3.setRole(r2);
r1.setAdmins(new Admin[] {a1});
r2.setAdmins(new Admin[] {a2,a3});
//设置角色和管理员组
r1.setGroups(new Group[] {g1,g2,g3,g4,g5});
r2.setGroups(new Group[] {g1,g2});
g1.setRoles(new Role[] {r1,r2});
g2.setRoles(new Role[] {r1,r2});
g3.setRoles(new Role[] {r1});
g4.setRoles(new Role[] {r1});
g5.setRoles(new Role[] {r1});
//设置管理员组和权限
g1.setActions(new Action[] {ac1,ac2,ac3});
g2.setActions(new Action[] {ac4,ac5,ac6});
g3.setActions(new Action[] {ac7,ac8,ac9});
g4.setActions(new Action[] {ac10,ac11});
g5.setActions(new Action[] {ac12,ac13});
ac1.setGroup(g1);
ac2.setGroup(g1);
ac3.setGroup(g1);
ac4.setGroup(g2);
ac5.setGroup(g2);
ac6.setGroup(g2);
ac7.setGroup(g3);
ac8.setGroup(g3);
ac9.setGroup(g3);
ac10.setGroup(g4);
ac11.setGroup(g4);
ac12.setGroup(g5);
ac13.setGroup(g5);
//取出数据内容
System.out.println(a1.getInfo());
System.out.println("\t|-"+a1.getRole().getInfo());
for(int i=0;i<a1.getRole().getGroups().length;i++) {
System.out.println("\t\t|-"+a1.getRole().getGroups()[i].getInfo());
for(int j=0;j<a1.getRole().getGroups()[i].getActions().length;j++) {
System.out.println("\t\t\t|-"+a1.getRole().getGroups()[i].getActions()[j].getInfo());
}
}
System.out.println("-------------------------------------");
//2
System.out.println(g2.getInfo());
for(int i=0;i<g2.getRoles().length;i++) {
System.out.println("\t|-"+g2.getRoles()[i].getInfo());
for(int j=0;j<g2.getRoles()[i].getAdmins().length;j++) {
System.out.println("\t\t|-"+g2.getRoles()[i].getAdmins()[j].getInfo());
}
}
System.out.println("-------------------------------------");
System.out.println("-------------------------------------");
}
}
class Admin{//管理员
private String aid;
private String password;
private Role role; //一个管理员属于一个角色
public Admin() {}
public Admin(String aid,String password) {
this.aid=aid;
this.password=password;
}
public String getAid() {
return aid;
}
public void setAid(String aid) {
this.aid = aid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setRole(Role role) {
this.role=role;
}
public Role getRole() {
return role;
}
public String getInfo() {
return "管理员编号:"+this.aid+",密码:"+this.password;
}
}
class Role{//角色
private int rid;
private String title;
private Admin[] admins;//一个角色有多个管理员
private Group[] groups;//一个角色对应多个权限组
public Role() {}
public Role(int rid,String title) {
this.rid=rid;
this.title=title;
}
public int getRid() {
return rid;
}
public void setRid(int rid) {
this.rid = rid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void setAdmins(Admin[] admins) {
this.admins=admins;
}
public Admin[] getAdmins() {
return admins;
}
public void setGroups(Group[] groups) {
this.groups=groups;
}
public Group[] getGroups() {
return groups;
}
public String getInfo() {
return "角色编号:"+this.rid+",名称:"+this.title;
}
}
class Group{//管理员组
private int gid;
private String title;
private Role[] roles;//一个管理员组有多个角色
private Action[] actions;//一个权限组有多个权限
public Group() {}
public Group(int gid,String title) {
this.gid=gid;
this.title=title;
}
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void setRoles(Role[] roles) {
this.roles=roles;
}
public Role[] getRoles() {
return roles;
}
public void setActions(Action[] actions) {
this.actions=actions;
}
public Action[] getActions() {
return actions;
}
public String getInfo() {
return "组编号:"+this.gid+",名称:"+this.title;
}
}
class Action{//权限
private int aid;
private String title;
private String url;
private Group group;//一个权限属于权限组
public Action() {}
public Action(int aid,String title,String url) {
this.aid=aid;
this.title=title;
this.url=url;
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public void setGroup(Group group) {
this.group=group;
}
public Group getGroup() {
return group;
}
public String getInfo() {
return "权限编号:"+this.aid+",名称:"+this.title+",路径:"+this.url;
}
}