Java基础(扩充中...)

1、++i:在i存储的值上增加1并向使用它的表达式返回增加后的值。                  先加再赋值                    

2、i++:对 i 增加1, 但返回的是原来未增加的值。循环中下次使用就会+1。      赋值再加

(原理参考https://zhuanlan.zhihu.com/p/40645506)虽然我看了还是没懂

(个人见解:首先要理解赋值的意思,就是传递。i++直接单独使用只是将这个值放在了寄存器,和没加一样,循环的时候才能体现效果)

3、&&:若第一个条件不满足,后面条件就不再判断。

4、&: 要对所有的条件都进行判断。

5、sum+=i 等价于 sum=sum+i。

6、static静态方法或变量可直接调用,非static需要实例化new后才能调用,方法不返回数据,可以使用void声明,没有void则需要return。

7、xx.xxx:就是调用xx类中的xxx方法。

8、this.xx:调用当前类中的xx。

9、==:进行的是数值比较,如果用于对象比较,比较的是两个内存的地址数值。

10、equals():是类所提供的一个比较方法,可以直接进行字符串内容的判断。

11、Java中的static关键字解析 https://www.cnblogs.com/dolphin0520/p/3799052.html

foreach:迭代输出

对于数组而言,一般都会使用for循环进行输出,但是在使用传统for循环输出的时候往往使用下标的形式进行数组元素的访问。

public class ArrayDemo{
    public static void main(String args[]){
        int data[] = new int[]{1,2,3,4,5};
        for (int x = 0; x < data.length; x++){
            System.out.println(data[x]);         
        }
    }
}

JDK1.5之后,为了处理下标对程序的影响,以免数组越界,参考了.NET中的设计,引入了一个增强型的for循环(foreach),利用foreach的语法结构可以自动获取数组中的每一个元素,避免下标访问。

语法结构

for(数据类型  变量 : 数组|集合){ }

最大特点在于可以将数组中的每一个元素的内容取出保存在变量里面,这样就可以直接通过变量获取数组内容,而避免下标的方式来获取。

public class ArrayDemo{
    public static void main(String args[]){
        int data[] = new int[]{1,2,3,4,5};
        for (int temp : data){    //自动循环,将data数组的每一个内容交给temp
            System.out.println(temp);         
        }
    }
}

10、java中,什么时候需要new来实例化?
当需要用到一个类(接口、抽象类除外)的时候,需要new来进行初始化,才可以调用该类的方法、属属性、变量等。
比如:

public class Main{
    public static void main(String[] args){
        Person p = new Person();
        System.out.println(p.num);
        p.getPrint();
    }
}
class Person{
    int num;
    public void getPrint(){
        System.out.println("hello Person!");
    }
}


11、Java中 主方法组成分析

public static void main(String args[])

public:描述的是一种访问权限,主方法是一切的开始点,一定是共用的。

static:程序的执行是通过类名称完成的,所以表示此方法是由类直接调用。

void:主方法是一切的起点,起点一开始就没有返回的可能。

main:是系统定义好的名称

String args[]:字符串的数组,可以实现程序启动参数的接收。

12、String类的常用方法

字符串与字符

package test;
public class example {
    public static void main(String[] args) {
        String str = "helloworld";
        char[] result = str.toCharArray() ;        //将字符串变为数组
        for(int x=0; x<result.length; x++){
            result[x] -=32;           //编码减少32
        }
        //将处理后的字符数组交给String变为字符串
        String newstr = new String(result);
        System.out.println(newstr) ;
        System.out.println(new String(result, 0, 5)) ;
      }
    }

13、实现字符串的数据检查

package test;
public class example {
    public static void main(String[] args) {
        String str = "helloworld";
        System.out.println(isNumber(str));
        System.out.println(isNumber("123"));
      }
      public static boolean isNumber(String str){
        char[] result = str.toCharArray();
        for(int x=0; x<result.length; x++){
            if(result[x] < '0' || result[x] > '9'){
                return false;
            }
        }
        return true;
      }
    }

14、字符串比较

package test;
public class example {
    public static void main(String[] args) {
        String strA = "m";
        String strB = "M";
        System.out.println(strA.compareToIgnoreCase(strB));
        System.out.println(strA.compareTo(strB));
      }
    }

 

 

package test;
public class example {
    public static void main(String[] args) {
        Member memA = new Member("A","张三" );
        Member memB = new Member("B","李四" );
        Role roleA=new Role(1L,"系统配置");
        Role roleB=new Role(2L,"备份管理");
        Role roleC=new Role(3L,"人事管理");
        Privilege priA = new Privilege(1000L,"系统初始化");
        Privilege priB = new Privilege(1001L,"系统系统还原");
        Privilege priC = new Privilege(1002L,"系统环境处理");
        Privilege priD = new Privilege(2000L,"备份员工数据");
        Privilege priE = new Privilege(2001L,"备份部门数据");
        Privilege priF = new Privilege(2002L,"备份公文数据");
        Privilege priG = new Privilege(3000L,"增加员工");
        Privilege priH = new Privilege(3001L,"编辑员工");
        Privilege priI = new Privilege(3002L,"浏览员工");
        Privilege priJ = new Privilege(3003L,"员工离职");
        // 增加角色与权限的关系
        roleA.setPrivileges(new Privilege[] {priA,priB,priC});
        roleB.setPrivileges(new Privilege[] {priD,priE,priF});
        roleC.setPrivileges(new Privilege[] {priG,priH,priI,priJ});
        //增加权限与角色对应
        priA.setRole(roleA) ;
        priB.setRole(roleA) ;
        priC.setRole(roleA) ;
        priD.setRole(roleB) ;
        priE.setRole(roleB) ;
        priF.setRole(roleB) ;
        priG.setRole(roleC) ;
        priH.setRole(roleC) ;
        priI.setRole(roleC) ;
        priJ.setRole(roleC) ;
        //增加用户与角色关系
        memA.setRoles(new Role[] {roleA,roleB}) ;
        memB.setRoles(new Role[] {roleA,roleB,roleC}) ;
        roleA.setMembers(new Member[] {memA,memB}) ;
        roleB.setMembers(new Member[] {memA,memB}) ;
        roleC.setMembers(new Member[] {memB}) ;
        //根据要求获取数据
        System.out.println("------通过用户查找信息------");
        System.out.println(memB.getInfo());
        for(int x=0;x<memB.getRoles().length; x++){
            System.out.println("\t|- "+memB.getRoles()[x].getInfo() ) ;
            for(int y=0;y<memB.getRoles()[x].getPrivileges().length;y++){
                System.out.println("\t\t|-" + memB.getRoles()[x].getPrivileges()[y].getInfo());
            }
        }
        System.out.println("------通过角色查找信息------");
        System.out.println(roleB.getInfo());
        System.out.println("\t|- "+"浏览此角色下的所有权限信息:") ;
        for(int x=0;x<roleB.getPrivileges().length;x++){
            System.out.print("\t\t|- " +roleB.getPrivileges()[x].getInfo());
        }
        System.out.println("\t|- "+"浏览此角色下的所有用户信息:") ;
        for(int x=0; x<roleB.getMembers().length; x++){
            System.out.println("\t\t|- "+roleB.getMembers()[x].getInfo());
        }
        System.out.println("------通过权限查找信息------");
        System.out.println(priA.getInfo());
        for(int x=0; x<priA.getRole().getMembers().length; x++){
            System.out.println("\t|"+priA .getRole() .getMembers() [x].getInfo() );
        }
    }
}
class Member{
    private String mid;
    private String name;
    private  Role roles[];
    public Member(String mid,String name){
        this.mid=mid;
        this.name=name;
    }
    public void setRoles(Role roles[]){
        this.roles = roles;
    }
    public Role[] getRoles() {
        return roles;
    }
    public String getInfo(){
        return "【用户信息】mid="+this.mid + "、名字=" + this.name;
    }
}
class Role{
    private long rid;
    private String title;
    private Member members[];
    private Privilege[] privileges;
    public Role(long rid,String title){
        this.rid=rid;
        this.title=title;
    }
    public void setMembers(Member members[]){
        this.members = members;
    }
    public Member[] getMembers(){
        return this.members;
    }
    public void setPrivileges (Privilege privileges[] ){
        this.privileges =privileges ;
    }
    public Privilege[] getPrivileges(){
        return this.privileges;
    }
    public String getInfo(){
        return "【角色信息】rid="+this.rid+"、title="+this.title;
    }
}
class Privilege{
    private long  pid;
    private String title;
    private Role role;
    public Privilege (long pid,String title){
        this.pid=pid;
        this.title=title;
    }
    public void setRole(Role role){
        this.role = role;
    }
    public Role getRole(){
        return this.role;
    }
    public String getInfo(){
      return "【权限信息】pid"+this.pid+"、title="+this.title;
    }
}

1、字符串比较

package test;
public class example {
    public static void main(String[] args) {
        String strA = "mldn";
        String strB = "mlDN";
        System.out.println("测试1:"+strB.compareTo(strA));
        System.out.println("测试2:"+"hello".compareTo("Hello"));
        System.out.println(strA.compareToIgnoreCase(strB));
      }
    }

2、字符串查找

package test;
public class example {
    public static void main(String[] args) {
        String str = "深情不及久伴";
        System.out.println(str.contains("爱") );
        System.out.println(str.contains("不及") );
        //JDK1.5之前只能用 indexOf 来查找,找到了返回位置数,没找到返回-1
        String str1 = "Bsciology";
        System.out.println("indexOf测试:" + str1.indexOf("l"));
        System.out.println("indexOf测试:" + str1.indexOf("a"));
        if(str1.indexOf("o") != -1){
            System.out.println("找到了");
        }else{
            System.out.println("没找到");
        }
      }
    }
package test;
public class example {
    public static void main(String[] args) {
        String str = "深情不及久伴";
        System.out.println(str.lastIndexOf("不",1) );
        System.out.println(str.indexOf("不") );
      }
    }

3、字符串开头结尾查找

package test;
public class example {
    public static void main(String[] args) {
        String str = "深情不及久伴";
        System.out.println(str.startsWith("深"));
        System.out.println(str.endsWith("伴"));
      }
    }

4、字符串替换

package test;
public class example {
    public static void main(String[] args) {
        String str = "helloworld";
        System.out.println(str.replaceAll("l","o"));
        System.out.println(str.replaceFirst("h","f"));
      }
    }

5、字符串拆分

package test;
public class example {
    public static void main(String[] args) {
        String str = "hello world hello www";
        String result[] = str.split(" ");
        for(int x=0; x<result.length; x++){
            System.out.println(result[x]);
        }
      }
    }
package test;
public class example {
    public static void main(String[] args) {
        String str = "hello world hello www";
        String result[] = str.split(" ",2);
        for(int x=0; x<result.length; x++){
            System.out.println(result[x]);
        }
      }
    }
package test;
public class example {
    public static void main(String[] args) {
        String str = "192.168.5.1";
        //  拆分不了,正则原因, 使用“\\”进行转义, \\=\
        String result[] = str.split("\\.");
        for(int x=0; x<result.length; x++){
            System.out.println(result[x]);
        }
      }
    }

6、字符串截取

public String substring(int beginIndex);    从指定索引截取到结尾

public String substring(int beginIndex , int endIndex);    截取指定范围中的子字符串

package test;
public class example {
    public static void main(String[] args) {
        String str = "Pain past is pleasure";
        System.out.println(str.substring(3));
        System.out.println(str.substring(3,8));
      }
    }

有时候开始和结束索引往往都是通过indexOf()方法计算的出来的。

package test;
public class example {
    public static void main(String[] args) {
        String str = "aaa-20201105-张三.jpg";
        int beginIndex = str.indexOf("-",str.indexOf("20201105"))+1;
        int endIndex = str.lastIndexOf(".");
        System.out.println(str.substring(beginIndex ,endIndex ) );
      }
    }

7、格式化字符串

 

package test;
public class example {
    public static void main(String[] args) {
        String name = "张三";
        int age = 18;
        double score = 98.141592654;
        String str = String.format("姓名:%s、年龄:%d、成绩:%5.2f。",name,age,score);
        System.out.println(str);
      }
    }

8、其他操作方法

package test;
public class example {
    public static void main(String[] args) {
        String strA = "www.baidu.com";
        String strB = "www.".concat("baidu").concat(".com");
        System.out.println(strB);
        System.out.println(strA == strB);
      }
    }

“""”和“null”不等,一个表示有实例化对象,一个表示没有实例化对象。isEmpty()主要是判断字符串的内容,所以一定要是在有实例化对象的时候进行调用。

package test;
public class example {
    public static void main(String[] args) {
        String str = "";
        System.out.println(str.isEmpty());  //true
        System.out.println("baidu".isEmpty());  //flase
      }
    }

trim():去除左右的空白字符串

package test;
public class example {
    public static void main(String[] args) {
        String str = "   Hello  World";
        System.out.println(str.length());
        String trimStr = str.trim() ;
        System.out.println(str);
        System.out.println(trimStr);
      }
    }
package test;
public class example {
    public static void main(String[] args) {
        String str = "Hello World!!!";
        System.out.println(str.toUpperCase() );   //转大写
        System.out.println(str.toLowerCase() );   //转小写
      }
    }

9、自定义首字母大写方法

package test;
class StringUtil{
    public static String initcap(String str){
        if(str == null || "".equals(str) ){
            return str;    //原样返回
        }
        if(str.length() == 1){
            return str.toUpperCase();
        }
        return str.substring(0,1).toUpperCase() +str.substring(1) ;
    }
}
public class example {
    public static void main(String[] args) {
        String str = "Hello World!!!";
        System.out.println(StringUtil.initcap("hello"));
        System.out.println(StringUtil.initcap("m"));
      }
    }

课时60:继承的实现

如果在Java的程序之中想要实现继承的关系,那么就必须依靠extends关键字来完成,此关键字的具体语法如下:

class 子类 extends 父类 {}

    特别需要注意的是,很多情况下会把子类称为派生类,父类成为超类(SuperClass)。

范例:

package test;
class Person{
    private String name;
    private int 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;
    }
}
class Students{
    private String name;
    private int age;
    private String school;
    public void setName(String name){
        this.name=name;
    }
    public void setSchool(String school){
        this.school = school;
    }
    public void setAge(int age){
        this.age=age;
    }
    public String getName(){
        return this.name;
    }
    public String getSchool(){
        return school = school;
    }
    public int getAge(){
        return this.age;
    }
}
class Student extends Person{//Student是子类
    //在子类中不定义任何的功能
}
public class example {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("林大强");   //父类Person定义
        stu.setAge(31);         //父类Person定义
        System.out.println("姓名:" + stu.getName() + "、年龄:" + stu.getAge() );
        }
      }

继承实现的主要目的是在于子类可以重用父类中的结构,并且也可以实现功能的扩充,那么同时强调了:子类可以定义更多的内容,描述的范围更小。

范例:子类扩充定义

package test;
class Person{
    private String name;
    private int 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;
    }
}
class Students{
    private String name;
    private int age;
    private String school;
    public void setName(String name){
        this.name=name;
    }
    public void setSchool(String school){
        this.school = school;
    }
    public void setAge(int age){
        this.age=age;
    }
    public String getName(){
        return this.name;
    }
    public String getSchool(){
        return school = school;
    }
    public int getAge(){
        return this.age;
    }
}
class Student extends Person{//Student是子类
    private String school;  //子类扩充的属性
    public void setSchool(String school){
     this.school=school;
    }
    public String getSchool(){
        return this.school;
    }
}
public class example {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("林大强");   //父类Person定义
        stu.setAge(31);         //父类Person定义
        stu.setSchool("家里蹲大学");
        System.out.println("姓名:" + stu.getName() + "、年龄:" + stu.getAge() + "、学校:" + stu.getSchool() );
        }
      }

课时61:子类对象实例化流程

package test;
class Person{
    public Person(){
        System.out.println("【Person父类】一个新的Person父类实例化对象产生了。");
    }
}
class Student extends Person{//Student是子类

    public Student(){ //构造方法
        super();  //写与不写效果一样
        //super()表示的就是子类构造调用父类构造的语句,只能放在子类构造方法的首行。
        //在默认情况下的实例化处理,子类只会调用父类中的无参构造方法。所以写不写super()区别不大
        //但是父类没有提供无参构造的情况下,就必须利用super()明确调用有参构造。
        System.out.println("【Student子类】一个新的Student实例化对象产生了。");
    }
}
public class example {
    public static void main(String[] args) {
        new Student();  //实例化子类
        }
      }

结论:无论如何折腾,在实例化子类对象的同时一定会实例化父类对象,目的是为了所有的属性可以进行空间的分配。

super与this都可以调用构造方法,super是由子类调用父类的构造,而this是调用本类的构造,并且都一定要放在构造方法的首行,所以两个语句不允许同时出现。

package test;
class Person{
    private String name;
    private int age;
    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }
    public String getInfo(){
        return "姓名:"+this.name + "、年龄"+this.age ;
    }
}
class Student extends Person{//Student是子类
    private String school;
    public Student(String name,int age,String school){
       super(name,age);
       this.school = school;
    }
}
public class example {
    public static void main(String[] args) {
        Student stu = new Student("暖光",3,"幼儿园");
        System.out.print(stu.getInfo());
        //默认打印的是对象的内存地址,所以输出是:test.Student@64c64813
        //重写Student的toString方法,咋个写?
        }
      }

我写不来了,就这样吧,越改错越多,只是想展示一下stu的输出,结果是内存地址,只好多加了一个getInfo进去,结果又少了school,唉。

Java 从入门到放弃

方法1,将private改成public

package test;
class Person{
    public String name;
    public int age;
    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }
}
class Student extends Person{
    private String school;
    public Student(String name,int age,String school){
       super(name,age);
       this.school = school;
    }
    public String toString(){
        return  super.name + super.age + this.school ;
    }
}
public class example {
    public static void main(String[] args) {
        Student h = new Student("暖光",5,"握草");
        System.out.print(h.toString() );
        //默认打印的是对象的内存地址,所以输出是:test.Student@64c64813
        //重写Student的toString方法?
        }
      }

Java中控制访问权限的关键字主要有4个:public、private、default、protected。
均可以修饰类、方法、变量。

1.public
内容对所有的类、所有对象都开放,可以被直接访问。

2.private
修饰的内容仅允许同一个类的成员访问。
修饰类中的全局变量时,内容仅允许同一个类中的成员访问。如果要在外部修改,则需要调用方法。
修饰方法中的局部变量时,只能被该方法访问。

由于对访问权限的高度限制,private常常用于保护数据,防止内存泄露。

3.default
default的权限介于public和private之间。它允许同一个包下的不同类访问。

4.protected
当构造ADT时,子类通常只会对父类的方法进行一部分的修改,所以需要使用父类中已经实现的一些方法。protected修饰的内容可以在子类中直接使用。
同时,protected还兼具了default的功能,允许同一个包下的不同类访问。

 

方法2,自己封装两个方法用于获取

package test;
class Person{
    private String name;
    private int age;
    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }
    public String getName(){
        return this.name ;
    }
    public int getAge(){
        return this.age;
    }
}
class Student extends Person{
    private String school;
    public Student(String name,int age,String school){
       super(name,age);
       this.school = school;
    }
    public String toString(){
        return  super.getName() + super.getAge() + this.school ;
    }
}
public class example {
    public static void main(String[] args) {
        Student h = new Student("暖光",5,"握草");
        System.out.print(h.toString());
        //默认打印的是对象的内存地址,所以输出是:test.Student@64c64813
        //重写Student的toString方法?
        }
      }

 

应该就这样了,这点问题花了我很多个小时,哈哈哈  也学到了很多东西。

 

课时62:继承定义限制

1、Java中不允许多重继承,只允许多层继承。理论上一般层次不超过三层

子类可以继承父类中的所有操作结构,但是对于私有操作属于隐式继承,所有的非私有操作属于显式继承。

多层继承

class A {}
class B extends A {}
class C extends B {}

继承一旦发生,所有的操作都可以被子类使用。

package test;
class Person{
    private String name;
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return this.name;
    }
}
class Student extends Person{
    public Student(String name){
        setName(name);
    }
    public void fun(){
        System.out.println(getName());
    }
}
public class example {
    public static void main(String[] args) {
         Student stu = new Student("哎呀") ;
         stu.fun() ;
        }
      }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值