栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java基础二刷

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java基础二刷

方法的重载:

举个例子,当我们执行多个返回值时a+b的方法时,如果我们采用多个方法名来辨别不同的方法,这样可能会导致程序员的记忆混乱,代码冗余,此时我们可以采用方法的重载这个知识点。

方法重载。通过形参的数量和数据类型来进行区分每个函数。

修改前的代码:

public class Demo {
    public static void  main(String[] args){
        int a=1,b=2;
        System.out.println(sum1(a,b));
        System.out.println(sum2(a,b));
    }
    public  static int sum1(int a, int b){
        return a+b;
    }
    public static double sum2(double a, double b){
        return a+b;
    }
}

相关结果:

运用到方法重载的相关代码:

public class Demo {
    public static void  main(String[] args){
        int a=1,b=2;
        System.out.println(a(a,b));
        System.out.println(a(a,b));
    }
    public  static int a(int a, int b){
        return a+b;
    }
    public static double a(double a, double b){
        return a+b;
    }
}

相关代码结果:

方法应该如何调用:
对应普通方法来说,如果方法用static来进行修饰的时候,可采取类名.方法名,如果没有用static来进行修饰方法的时候,可采取对象名.方法名。

构造方法的重载:

构造方法是通过构造对象时,给该对象赋初值,同时构造方法支持方法的重载。

参数传递:

如果是局部变量进行传递参数,他不会影响主方法中的变量。毕竟不是地址传递,他只是改变变量表面的值,地址并没有进行连接。如果传入的参数涉及到引用类型,比如String,和下面的Demo类型,那么就会就会发生一定的改变。

值传递的相关代码:

public class Demo{

    public static void main(String[] args){
        int a=2,b=4;
        Demo.fun(a,b);
        System.out.println(a+" "+b);
    }
    public static void fun(int a,int b){
        ++a;
        ++b;
        System.out.println(a+" "+b);
    }
}

引用传递的相关代码:

public class Demo{
    int age=11;
    public static void main(String[] args){
        Demo d = new Demo();
        System.out.println("d.age="+d.age);
        Demo.fun(d);
        System.out.println("d.age="+d.age);

    }
    public static void fun(Demo demo){
        demo.age=12;
    }
}

this关键字的使用:
this可以理解成该类的实例化对象或者理解成正在用构造器创建的实例化对象,this随着对象的不同而不同,它不是固定的。

public class Demo {

    public static void main(String[] args){
        Student s = new Student("1000",20);
        System.out.println(s.number);
        System.out.println(s.score);
        s.run();
    }
}

class Student{
    String number;
    int score;
    Student(String number,int score){
        this.number = number;
        this.score = score;
    }
    public void run(){
        fun();   //其实这个地方少了一个this,应该是this.run().
    }
    public void fun(){
        System.out.println("学生要去跑步");
    }
}

继承:

继承是子类继承了父类的属性和方法,在子类在不改变原有的属性和方法时,可以通过子类来调用父类的属性和方法。在子类改变原有的属性和方法并且多增加几个新属性和方法时,可以直接调用子类的属性和方法,不用去调用父类。通过下面的例子就可以得出。

相关代码:

public class Demo {

    public static void main(String[] args){
        System.out.println(new Student().name);
        System.out.println(new Teacher().age);
    }
}

class Student extends  Person{
    int score=2;
    String subject="语文";
    String name="帅学生";

}
class Teacher extends Person{
    String  work="教语文";
}
class Person {
    String name="野人";
    int age=38;
}

相关代码结果:

方法的重写:
当子类对父类的方法不满意时,子类可以进行重写该方法。

相关代码:

public class Demo {

    public static void main(String[] args){
        Student s = new Student();
        s.work();
    }
}

class Person {
    void work(){
        System.out.println("人类都应该工作!");
    }
}

class Student extends Person{
    void work(){
        System.out.println("学生都应该工作!");
    }
}

相关代码结果:

属性的重写:

不仅方法可以重写,属性也可以重写。

相关代码:

public class Demo {

    public static void main(String[] args){
        Student s = new Student();
        s.work();
        System.out.println(s.id);
    }
}

class Person {
    int id=1000;
    void work(){
        System.out.println("人类都应该工作!");
    }
}

class Student extends Person{
    int id=1002;
    void work(){
        System.out.println("学生都应该工作!");
    }
}

相关代码结果;

super关键字:
super表示的是父类的引用对象,super.属性名      super.方法名通过这两种方式可以调用父类的属性和方法。和this关键字的用法类似。

相关代码:

public class Demo {

    public static void main(String[] args){
        Student s = new Student();
        s.work1();
        s.work2();
        System.out.println(s.id);
        s.fun();
    }
}

class Person {
    int id=1000;
    void work1(){
        System.out.println("人类都应该工作!");
    }
}

class Student extends Person{
    int id=1002;
    void work1(){
        System.out.println("学生都应该工作!");
    }
    void work2(){
        super.work1();
    }
    void fun(){
        System.out.println("super.id="+super.id);
    }
}

相关代码结果:

关于构造器中的super():
构造器中的super(),它是为了将父类的属性进行修改,但是不会修改子类的属性。super()是对父类的属性进行修改,同时子类可以引用父类的属性。并且构造器中默认有super(),下面这个例子可以体现出。

相关代码:

public class Demo {

    public static void main(String[] args){
        Student s = new Student(121,"student",12121,50);
        System.out.println(s.id);
        System.out.println(s.name);
        System.out.println(s.number);
        System.out.println(s.age);
        s.show();
        
    }
}

class Person {
    int id=1000;
    int age=100;
    String name="野人";
    public Person(int id,int age){
        this.id=id;
        this.age=age;
        System.out.println("调用了父类的构造器");
    }
    public Person(){
        System.out.println("调用了父类的构造器");
    }

}

class Student extends Person{
    int id=1002;
    String name="学生";
    int number=121212;
    public Student(int id,String name,int number,int age){
        super(id,age);
       this.name=name;
       this.number = number;
        System.out.println("调用了子类的构造器");
    }
    void show(){
        System.out.println(super.id+" "+super.age+" "+super.name);
    }
}

相关代码:

对多态的理解:
        当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有该方法,我们可以将父类强制转换成子类,然后去调用子类中的方法,如果用父类直接调用方法,则会编译错误;如果有该方法,再去调用子类的同名方法。如果子类没有方法,则会直接调用父类的该方法。除此之外,多态对方法适用,但是对属性并不适用。通过下面的例子,我们可以对多态进行更深一步地了解。

相关代码:

public class Demo {
    public static void main(String[] args){
       Person p = new Student();
       p.work();
       Student s = (Student) p;
       s.fun();
       s.work();
       s.age();
       System.out.println(p.id);
       System.out.println(s.id);
    }
}
class Person {
    int id=1000;
    void work(){
        System.out.println("人类应该去工作");
    }
    void age(){
        System.out.println("人们都应该长命百岁");
    }
}

class Student extends Person{
    int id=1001;
    @Override
    void work() {
        System.out.println("学生应该学习");
    }
    void fun(){
        System.out.println("学生应该都门门考高分");
    }
}

相关代码结果:

==和equal()方法:

对于==来说,如果是基本数据类型的比较,那就是比较他们的数值,如果是引用数据类型的比较,那就是比较他们的地址值。

对于equal()方法来说,他原方法是比较他们的地址值,但是在String引用类型中,equal()方法进行了重写。

像String,File,Date等包装类都重写了equals()方法。

相关代码:
 

public class Demo {

    public static void main(String[] args){
        String a = new String("123");
        String b = new String("123");

        if(a==b) System.out.println("true");
        else System.out.println("false");
        if(a.equals(b)) System.out.println("true");
        else System.out.println("false");
    }
}

相关代码结果:

equals()方法的重写:

看代码吧,这里还是比较好理解的。

相关代码:

public class Demo{

    public static void main(String[] args){
        Person p1 = new Person("tom",21);
        Person p2 = new Person("tom",21);
        System.out.println(p1.equals(p2));
    }

}

class Person {
    String name;
    int age;
    Person(String name,int age){
        this.name=name;
        this.age=age;
    }

    public boolean equals(Object obj){
        if(obj == this) return true;
        if(obj instanceof Person){    //左边的变量是不是右边的实例化对象
            Person t = (Person) obj;
            if(t.name==this.name&&t.age==this.age) return true;
            else return false;
        }
        return false;
    }
}

相关代码结果:

toString()函数的使用:
当我们输出一个函数的引用的时候,其实就相当于调用了该对象的toString()方法. 像String,Date,File等包装类都重写了toString()函数方法。都可以直接打出数值,而不是地址。

相关代码:

public class Demo{

    public static void main(String[] args){
        Person p = new Person(20);
        System.out.println(p.toString());
        System.out.println(p);
    }
}

class Person {
    int age;
    Person(int age){
        this.age = age;
    }
}

相关代码结果:

手动实现toString()方法的重写:

其实很简单,看了代码自己应该可以理解。

public class Demo{

    public static void main(String[] args){
       Person p = new Person(12);
       System.out.println(p.toString());
    }
}

class Person {
    int age;
    Person(int age){
        this.age = age;
    }
    public String toString(){
        String s = "the person age is " ;
        String s1 = Integer.toString(this.age);
        return s+s1;
    }
}

相关代码结果:

static关键字:
当使用static来修饰变量时,该变量的值所有的对象可以共享,并且都可以对它进行修改。

相关代码:

public class Demo {

    public static void main(String[] args){
        Person p1 = new Person("马龙",30,"china");
        Person p2 = new Person("吴帅",20,"chin");
        System.out.println(p1.nation);
        System.out.println(p2.nation);
    }
}
 class Person {
    String name;

     public Person(String name, int age, String nation) {
         this.name = name;
         this.age = age;
         this.nation = nation;
     }

     int age;
    static String nation;

}

相关代码结果:


 

 当用static来修饰方法时,可以直接通过类名.方法来进行调用,在静态方法中不能直接调用非静态方法。而在非静态方法中可以直接调用静态方法,或者可以在静态方法中调用静态方法。

相关代码:
 

public class Demo {

    public static void main(String[] args){
        Person.show();
    }
}
 class Person {
    String name;

     public Person(String name, int age, String nation) {
         this.name = name;
         this.age = age;
         this.nation = nation;
     }

     int age;
     String nation;
     static void show(){
        System.out.println("人类都喜欢炫耀!");
     }
      void work(){
         show();
     }

}

代码相关结果:

abstract抽象类:

此类不能实例化,抽象类中一定有构造器,便于子类进行调用来实例化。抽象类中可能没有抽象方法。子类可以调用抽象类中的非抽象方法,同时还可以增加自己的新方法。

相关方法:

public class Demo {

    public static void main(String[] args){
        Person s =new Student();
        s.show();
        s.work();
    }
}

class Student extends Person {
    void work(){
        System.out.println("学生都需要认真学习");
    }
}
abstract class Person {
    void show(){
        System.out.println("人类都喜欢炫耀!");
    }

    abstract void work();
}

代码相关结果:

interface关键字:

接口中不能存在构造器,意味着接口不能进行实例化,接口由全局常量,抽象方法所构成。   子类必须要重写接口中所有的抽象方法才能够进行实例化。

相关代码:

public class Demo {
    public static void main(String[] args){
        Person p = new Student();
        System.out.println(p.id);      //常量修改不了
        p.work();  
    }
}
interface Person {
    int id=100;
    abstract void work();
}

class Student implements Person{
    int id=1001;
    @Override
    public void work() {
        System.out.println("学生应该学习");
    }
}

代码相关结果:

异常:

try-catch-finally      throws 异常类型

        第一种方式我们可以看做成狼来的故事,当此之前一切安好,表示程序正常进行,当狼来的时候,表示出现了异常,此时小孩看到这只狼很小,随后自己把狼给杀了,表示进行catch处理。

        第二种方式同样引用狼来的故事, 当小孩发现自己打不赢狼,小孩摇人,随后几个人过来把狼给杀了。

String类:

String是用final来声明的,表示不可被继承。String内部定义了final char value[] 用来存储字符串。String同时也代表了不可变性。

如果是类似于String s = "123";来进行赋值的,字符串常量池中就会多出一个"123"的字符串,而字符串常量池中每个字符串是唯一的,不可重复。   而如果是类似于String s = new String("123")来进行赋值的,堆空间中就会有一块内存中存放"123"。通过下面的例子可以进行更深一步地了解。

相关代码:

public class Demo {

    public static void main(String[] args){
        String s1 = "121";
        String s3 = "121";
        String s2 = new String("123");
        String s4 = new String("123");
        System.out.println(s1==s3);
        System.out.println(s2==s4);
    }
}

相关代码结果:

Java的常用方法:

请看相关代码。

相关代码:
 

import java.util.Locale;

public class Demo {

    public static void main(String[] args){
        String s = "123456789";
        System.out.println(s.length());      //字符串的长度
        System.out.println(s.charAt(0));     //返回下标为0的字符
        System.out.println(s.isEmpty());     //判断s是否是空字符串,如果是,则为true
        s = "ABC D";
        String s1 = s.toLowerCase();   //将s的字符串全部改成小写
        System.out.println(s);   //s是不可变的,仍然是原来的字符串。
        System.out.println(s1);

        s = "abc d12";
        String s2 = s.toUpperCase(); //将s的字符串全部改成大写
        System.out.println(s);   //s是不可变的,仍然是原来的字符串。
        System.out.println(s2);

        s = "   hel l  o w  o  r   l  d        ";
        String s3 = s.trim();   //将s的字符串的前导空格和最后面的空格全部除去。
        System.out.println(s);   //s是不可变的,仍然是原来的字符串。
        System.out.println(s3);

        s = "helloworld";
        String s4 = "HeLLoWorlD";
        System.out.println(s.equalsIgnoreCase(s4)); //在忽略大小写的情况下,判断这两个字符串是否数值成相等。

        s = "abc";
        String s5 = "abc";
        System.out.println(s.compareTo(s5));  //将s和s5比较,如果s比较小,则返回-1,如果大,则返回1,如果相等,则返回0.

        s = "abcdefghijklmn";
        String s6 = s.substring(1,5);  //截取下标从1到5-1的字符子串。
        System.out.println(s6);

        s = "hello-world";
        System.out.println( s.contains("hell"));   //"hell"是否是s的子串,如果是,则返回true。

        s = "hello-world";
        System.out.println(s.indexOf("lo"));   //返回子串在主串中第一次出现的下标,如果没有,则返回-1

        s = "hello-world";
        String s7 = s.replace('-',' ');    //将字符串中的oldchar换成newchar

        String s8 = s.replace("-"," ");  //将字符串中的oldString换成newString
        System.out.println(s7);
        System.out.println(s8);

        //将数字字符串全部改成数字类型。
        s="12345";
        int temp1 = Integer.parseInt(s);
        System.out.println(temp1);

        //将数字类型转换成字符串类型
        String s9 = String.valueOf(temp1);
        System.out.println(s9);

        s = "hello-world";
        char c[] = s.toCharArray();   //将字符串转换成字符数组。
        //数组的长度是没有(),字符串的长度是有()的。
        for(int i=0;i 

StringBuffer常用方法:

请看相关代码:

public class Demo {

    public static void main(String[] args){
        String s1="123";
        StringBuffer s = new StringBuffer(s1);
        s.append('1');  //增加字符
        s.append('3');
        System.out.println(s);
        System.out.println(s.length());   //计算字符串的长度

        StringBuffer s2 = new StringBuffer(s1);
        s.reverse();   //进行字符串颠倒
        System.out.println(s);

        StringBuffer s3 = new StringBuffer(s1);
        System.out.println(s.indexOf("1"));    //字符串模式匹配算法

        StringBuffer s4 = new StringBuffer(s1);
        System.out.println(s4.substring(1,3));   //截取字符串从下标为1开始,下标3-1结束的子串
        System.out.println(s4.charAt(1));    //返回下标为1的字符
        s4.delete(0,2);  //删除从下标为0开始到下标为2-1的子串
        System.out.println(s4);

        StringBuffer s5 = new StringBuffer(s1);
        s5.deleteCharAt(1); //删除下标为1的字符。
        System.out.println(s5);
    }
}

自定义排序:

用comparator接口来实现排序。

相关代码:

import java.util.Arrays;
import java.util.Comparator;

public class  Demo {

    public static void main(String[] args){
        Person a[] = new Person[3];
        a[0]=new Person("tom",21);
        a[1]=new Person("tom",29);
        a[2]=new Person("tom",25);
        Arrays.sort(a, new Comparator() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.age-o1.age;    //o2可以看成前面的对象,o1可以看成后面的对象。
            }
        });
        for(int i=0;i<3;i++){
            System.out.println(a[i].age);
        }
    }
}

class Person{
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

高精度整数:
BigInteger ,这个类在math包中,可以实现高精度的加减乘除,就以高精度加法为例。

相关代码:

import java.util.*;
import java.math.*;
public class Demo {

    public static void main(String[] args){
        String t1;
        String t2;
        Scanner scan = new Scanner(System.in);
        t1 = scan.next();
        t2 = scan.next();
        BigInteger s1 = new BigInteger(t1);
        BigInteger s2 = new BigInteger(t2);
        System.out.println(s1.add(s2));
    }
}

集合:
ArrayList的相关方法,请看相关代码。

相关代码:
 

import java.util.*;

public class Demo {

    public static void main(String[] args){
        ArrayList c = new ArrayList();
        c.add(1);
        c.add("1");
        System.out.println(c.size());
        System.out.println(c);   //由于ArrayList中toString进行了重写。
        System.out.println(c.isEmpty());     //判断当前集合是否为空
        c.clear();    //清空集合内所有的元素
    }
}

class Person {

}

对contains()方法的理解:

contains(Object o)方法调用的是o的equals()方法
由于String类中进行了重写,对于自定义的类来说,
需要重写equals()方法。

相关代码:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;

public class Demo {

    public static void main(String[] args){
        Collection c  = new ArrayList();
        c.add(new String("tom"));
        c.add(new Person(12,32));
        System.out.println(c.contains(new String("tom")));
        System.out.println(c.contains(new Person(12,32)));
    }
}

class Person {
    int id;
    int age;
    Person(int id,int age){
        this.age = age;
        this.id = id;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return id == person.id && age == person.age;
    }
}

相关代码结果:

c.remove(b)   //如果移除成功,则返回true remove()方法与contains()方法类似,自定义的类都需要重写equals()方法。

将集合转化成数组:
请看相关代码:

相关代码:

import java.util.ArrayList;
import java.util.Collection;

public class Demo {

    public static void main(String[] args){
        Collection c  = new ArrayList();
        c.add(1);
        c.add(2);
        c.add(3);
        Object a[] = c.toArray();
        for(int i=0;i

迭代器遍历:

相关代码:

import java.util.ArrayList;
import java.util.*;

public class Demo {

    public static void main(String[] args){
       Collection  c = new ArrayList();
       c.add(1);
       c.add(2);
       c.add(3);
       Iterator it = c.iterator();
       while(it.hasNext()){
           System.out.println(it.next());
       }
    }
}

LinkedList:对于频繁插入,删除元素的集合。使用LinkedList的效率比ArrayList的高。底层使用的是双向链表进行存储。

Set接口:
Set接口中的方法都是Collection接口中所声明的方法。

HashSet是无序的。遍历也是无序的。同时也具有不可重复性。

相关代码:

import java.util.ArrayList;
import java.util.*;

public class Demo {

    public static void main(String[] args){
       Set  c =  new HashSet();
       c.add(1);
       c.add(2);
       c.add(3);
       c.add(-211);
       c.add(5);
       Iterator it = c.iterator();
       while(it.hasNext()){
           System.out.println(it.next());
       }
    }
}

Set为什么去重时,需要重写hashCode和equals方法?

HashSet的底层是HashMap,首先要通过hashcode()来确定哈希值,即确定所在的位置,equals()来确定元素的字面量是否相同。HashSet的唯一性是通过HashMap的键值来实现的。

重写之前的代码:

import java.util.ArrayList;
import java.util.*;

public class Demo {

    public static void main(String[] args){
       Set  c =  new HashSet();
       c.add(new Person("tom",12));
       c.add(new Person("tom",12));
       System.out.println(c);
    }
}

class Person {
    String name;
    int age;

    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }
}

相关结果:

重写之后的代码:

import java.util.ArrayList;
import java.util.*;

public class Demo {

    public static void main(String[] args){
       Set  c =  new HashSet();
       c.add(new Person("tom",12));
       c.add(new Person("tom",12));
       System.out.println(c);
    }
}

class Person {
    String name;
    int age;

    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

相关结果:

TreeSet的自然排序:
该集合内的元素必须是同一类,并且该集合内的元素会自动排序。 

map:

map中的key是不重复的,无序的,是用Set来储存key。

value是无序的,但是是可重复的。是用Collection来储存value。

map中的键值对是无序的,不可重复的。

相关方法:

HashMap h = new HashMap();
        h.put(12,12);
        h.put("12","31");
        h.remove(12);    //删除键值为12的键值对
        h.clear(); //清除h中所有的键值对
        System.out.println(h.size());   //计算h的长度
        System.out.println(h.get(12));   //获取键值为12的value
        //containsKey( key)    是否包含指定的key
        //containsValue(value)    是否包含指定的value
        //isEmpty()   判断此时的map是否为空

泛型这里我觉得应该在实战中来学习是比教好的,听概念会比较懵。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/924806.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号