Java面向对象编程_1
总览:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cc316437097404760a7696fbecf2367a.png)
1,方法
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7c30788d7f67c00101675d07ebd5eb5a.png)
2,类与对象
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f8fa0a36370d01eb63ecbff2cd2e9562.png)
demo_static方法
class data
{
private int num;
private String job;
private String spot;
static String country="中华民国";
public static String countryInfo()
{
return "国籍:"+country;
}
public data(){}
public data(int num,String job,String spot)
{
this.num=num;
this.job=job;
this.spot=spot;
}
public void setNum(int num)
{
this.num=num;
}
public void setJob(String job)
{
this.job=job;
}
public void setSpot(String spot)
{
this.spot=spot;
}
public int getNum()
{
return this.num;
}
public String getJob()
{
return this.job;
}
public String getSpot()
{
return this.spot;
}
public String dataInfo()
{
return "编号:"+this.num+" 部门:"+this.job+" 地点:"+this.spot+countryInfo();
}
}
public class Static
{
public static void main(String agrs[])
{
System.out.println( data.countryInfo());
data a=new data(001,"财务科","北京");
a.country="中华人民共和国";
System.out.println(a.dataInfo());
}
}
demo_代码块
class A
{
static {
System.out.println("【静态代码块】在子类中用于初始化,先于构造块执行");
}
{
System.out.println("【构造代码块】在子类中用于初始化,先于构造方法执行");
}
public A(){
System.out.println("【构造方法】执行");
}
}
public class 代码块
{
static{
System.out.println("【静态代码块】在主类中用于初始化,先于main方法执行");
}
public static void main(String agrs[]){
{
int a=100;
System.out.println("【普通代码块】"+a);
}
int a=0;
new A();
}
}
demo_简单Java类
class Employee
{
private String empnum;
private String empname;
private double empsalary;
private double emprate;
public Employee(){}
public Employee(String empnum,String empname,double empsalary,double emprate){
this.empnum=empnum;
this.empname=empname;
this.empsalary=empsalary;
this.emprate=emprate;
}
public String empInfo(){
return "编号:"+empnum+" 姓名:"+empname+" 薪水:"+empsalary+" 增长率:"+emprate;
}
public String salaryInfo(){
return " 薪水增长值:"+(this.empsalary * this.emprate)+" 增长后的薪水:"+(this.empsalary*(1+this.emprate));
}
}
public class 简单java类_2{
public static void main(String agrs[]){
Employee emp=new Employee("0001","李华",2000,0.2);
System.out.println(emp.empInfo()+emp.salaryInfo());
}
}
3,数组
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d97d1c4b4e4596c39be3be667631570e.png)
demo_二维数组
public class 二维数组
{
public static void main(String agrs[]){
int data[][]=new int [][]{
{1,2,3},{4,5,6,7,8},{9,10}
};
for(int temp[]:data){
for(int num:temp){
System.out.print(num);
}
System.out.println();
}
}
}
demo_数组与方法
class Array{
private int max;
private int min;
private int sum;
private int i;
public Array(){}
public Array(int data[]){
this.max=data[0];
this.min=data[0];
for(i=0;i<data.length;i++){
this.sum+=data[i];
if(this.max<data[i])
this.max=data[i];
if(this.min>data[i])
this.min=data[i];
}
}
public String arrayInfo(){
return "最大值:"+this.max+" 最小值:"+this.min+" 总和:"+this.sum+" 平均值:"+(this.sum/(i));
}
}
public class 数组与方法
{
public static void main(String agrs[]){
int data[]=new int[]{1,2,3,4,5};
System.out.println(new Array(data).arrayInfo());
}
}
demo_数组倒置
class Array
{
public static void arrayAction(int data[]){
int head=0;
int tail=data.length-1;
for(int i=0;i<data.length/2;i++){
int temp=data[head];
data[head]=data[tail];
data[tail]=temp;
head++;
tail--;
}
}
public static void printArray(int data[]){
for(int temp :data){
System.out.print(temp);
}
}
}
public class 数组倒置
{
public static void main(String agrs[]){
int a[]=new int[]{1,2,3,4,5,6,7};
Array.arrayAction(a);
Array.printArray(a);
}
}
demo_数组相关类库
class Array
{
public static void printArray(int data[]){
for(int temp:data){
System.out.print(temp+" ");
}
}
}
public class 数组相关类库
{
public static void main(String agrs[]){
int a[]=new int[]{54,84,92,84,2,69,89,102};
java.util.Arrays.sort(a);
Array.printArray(a);
int b[]=new int[]{1,2,3,4,5};
int c[]=new int[]{11,22,33,44,55};
System.arraycopy(c,1,b,1,2);
Array.printArray(b);
}
}
demo_对象数组
class person
{
private String name;
private int age;
public String personInfo(){
return "姓名:"+this.name+" 年龄:"+this.age;
}
public person(String name,int age){
this.name=name;
this.age=age;
}
}
public class 对象数组
{
public static void main(String agrs[]){
person per[]=new person[]{new person("a",1),new person("b",2),new person("c",3)};
for(int i=0;i<per.length;i++){
System.out.println(per[i].personInfo());
}
}
}
demo_类关联结构
class Car
{
private String name;
private double price;
private Person person;
public Car(String name,double price){
this.name=name;
this.price=price;
}
public void setPerson(Person person){
this.person=person;
}
public String carInfo(){
return "车辆型号:"+this.name+" 价格:"+this.name;
}
public Person getPerson(){
return this.person;
}
}
class Person
{
private String name;
private int age;
private Car car;
private Person children[];
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void setCar(Car car){
this.car=car;
}
public String personInfo(){
return "姓名:"+this.name+" 年龄:"+this.age;
}
public Car getCar(){
return this.car;
}
public void setChildren(Person children []){
this.children=children;
}
public Person[] getChildren(){
return this.children;
}
}
public class 类关联结构
{
public static void main(String agrs[]){
Person per=new Person("张三",18);
Person childA=new Person("a",10);
Person childB=new Person("b",11);
childA.setCar(new Car("车1",20.0));
childB.setCar(new Car("车2",30.0));
per.setChildren(new Person[]{childA,childB});
Car car=new Car("劳斯莱斯",2000000000.0);
per.setCar(car);
car.setPerson(per);
System.out.println(per.getCar().carInfo());
System.out.println(car.getPerson().personInfo());
for(int i=0;i<per.getChildren().length;i++){
System.out.println(per.getChildren()[i].personInfo());
System.out.println(per.getChildren()[i].getCar().carInfo());
}
}
}
demo_类关联结构2
class Person
{
private int pid;
private String name;
private Role role[];
public Person(int pid,String name){
this.pid=pid;
this.name=name;
}
public String getInfo(){
return "用户编号:"+this.pid+" 用户姓名:"+this.name;
}
public void setRole(Role[] role){
this.role=role;
}
public Role [] getRole(){
return this.role;
}
}
class Role
{
private int rid;
private String name;
private Person person[];
private Privilege privilege[];
public Role(int rid,String name){
this.rid=rid;
this.name=name;
}
public String getInfo(){
return "角色编号:"+this.rid+" 角色名称:"+this.name;
}
public void setPerson(Person[] person){
this.person=person;
}
public Person [] getPerson(){
return this.person;
}
public void setPrivilege(Privilege[] privilege){
this.privilege=privilege;
}
public Privilege [] getPrivilege(){
return this.privilege;
}
}
class Privilege
{
private int id;
private String name;
private Role role;
public Privilege(int id,String name){
this.id=id;
this.name=name;
}
public String getInfo(){
return "权限编号:"+this.id+" 权限名称:"+this.name;
}
public void setRole(){
this.role=role;
}
public Role getRole(){
return this.role;
}
}
public class 类关联结构_2
{
public static void main(String agrs[]){
Person per1=new Person(1,"张三");
Person per2=new Person(2,"李四");
Role role1=new Role(1001,"系统配置员");
Role role2=new Role(2001,"系统备份员");
Role role3=new Role(3001,"人事管理员");
Privilege pri1=new Privilege(1001,"系统采购");
Privilege pri2=new Privilege(1002,"系统安装");
Privilege pri3=new Privilege(2001,"系统维护");
Privilege pri4=new Privilege(2002,"系统备份");
Privilege pri5=new Privilege(3001,"人员招聘");
Privilege pri6=new Privilege(3002,"人员离职");
per1.setRole(new Role[]{role1,role2});
per2.setRole(new Role[]{role1,role3});
role1.setPerson(new Person[]{per1,per2});
role2.setPerson(new Person[]{per1});
role3.setPerson(new Person[]{per2});
role1.setPrivilege(new Privilege[]{pri1,pri2});
role2.setPrivilege(new Privilege[]{pri3,pri4});
role3.setPrivilege(new Privilege[]{pri5,pri6});
pri1.setRole(role1);
pri2.setRole(role1);
pri3.setRole(role2);
pri4.setRole(role2);
pri5.setRole(role3);
pri6.setRole(role3);
System.out.println("-----------------------以用户搜索角色与其对应权限---------------------");
System.out.println(per1.getInfo());
for(int i=0;i<per1.getRole().length;i++){
System.out.println("\t|-"+per1.getRole()[i].getInfo());
for(int j=0;j<per1.getRole()[i].getPrivilege().length;j++){
System.out.println("\t\t|-"+per1.getRole()[i].getPrivilege()[j].getInfo());
}
}
System.out.println(per2.getInfo());
for(int i=0;i<per2.getRole().length;i++){
System.out.println("\t|-"+per2.getRole()[i].getInfo());
for(int j=0;j<per2.getRole()[i].getPrivilege().length;j++){
System.out.println("\t\t|-"+per2.getRole()[i].getPrivilege()[j].getInfo());
}
}
System.out.println("-----------------------以角色搜索用户---------------------");
System.out.println(role1.getInfo());
for(int i=0;i<role1.getPerson().length;i++){
System.out.println("\t|-"+role1.getPerson()[i].getInfo());
}
System.out.println(role2.getInfo());
for(int i=0;i<role2.getPerson().length;i++){
System.out.println("\t|-"+role2.getPerson()[i].getInfo());
}
System.out.println(role3.getInfo());
for(int i=0;i<role3.getPerson().length;i++){
System.out.println("\t|-"+role3.getPerson()[i].getInfo());
}
System.out.println("-----------------------以角色搜索权限---------------------");
System.out.println(role1.getInfo());
for(int i=0;i<role1.getPrivilege().length;i++){
System.out.println("\t|-"+role1.getPrivilege()[i].getInfo());
}
System.out.println(role2.getInfo());
for(int i=0;i<role2.getPrivilege().length;i++){
System.out.println("\t|-"+role2.getPrivilege()[i].getInfo());
}
System.out.println(role3.getInfo());
for(int i=0;i<role3.getPrivilege().length;i++){
System.out.println("\t|-"+role3.getPrivilege()[i].getInfo());
}
}
}
4,String类
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/addf512666ee1aecdee496c987d2a92f.png)
demo_字符串池
public class String类
{
public static void main(String agrs[]) {
for(String temp:agrs){
System.out.println(temp);
}
String str1="ok";
String str2=new String("ok");
System.out.println(str1==str2);
System.out.println(str1.equals(str2));
String strA="ok";
String strB=new String("ok");
System.out.println(strA==strB);
String strC=new String("ok").intern();
System.out.println(strA==strC);
String stra="hello word";
String strb="hello"+" word";
String info="hello";
String strc=info+" word";
System.out.println(stra==strb);
System.out.println(stra==strc);
}
}
demo_字符串常用方法
public class String类常用方法
{
public static void main(String agrs[]){
}
}