zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java自学笔记

JAVA笔记 自学
2023-09-11 14:17:26 时间

  从去年就想学习后端的知识 前端的毕竟还学不完, 而且我从百度网盘下了好几十G的视频, 和一些电子书, 可惜了平常都没去看, 这两天才开始学习java, 这里作为自己的笔记, 学习永远不会太晚, 自学的资料是 《《java自学之道》》   ==>>  《Java自学之道》,一本java入门参考书籍,讲解中使用大量生活实例,让java学习更加通俗易懂,在开源中国开放不到一天下载量已超过3000人次。 开源中国下载地址:http://www.oschina.net/news/42748/java-self-study-guide;

  以前钟毅总说java是面向对象的语言, js也可以是面向对象的语言;

public class Person {
    int id;
    int age = 20;
    //默认的void Person为实例方法;
    //如果加了static那就是构造函数方法;
    void Person(int _id , int _age) {
        id = _id;
        age = _age;
        System.out.println("id = " + id + "\nage =" + age);
    };
    public static void main(String[] args) {
        //执行了new Person跟当前的方法一点关系都没有;
        //在JS里相当于只是调用了构造函数的方法, 
        //实例化这个构造函数而已;
        Person tom = new Person();
        tom.Person(1, 25);
    //我们也可以直接访问到这个age
    
System.out.println( tom.age );

    };
};

/*
    在JS中就写法如下:    
*/

Function Person() {
    var id,
        age;

    this.Person = function(_id, _age) {
        id = _id;
        age = _age;
        console.log(_id+""+_age);
    };
};
Person.run = function() {
    return (new Person()).Pserson(1,25);
};

   我用js也模拟写了下, 区别就是java的会自动执行一个main方法, 而在js中我们必须手动执行Person.run然后才能实例化构造函数, 以及定义函数的方式, 定义函数的参数, 构造函数内部变量的作用域会有一些区别;

  

  java的继承:

class FatherClass {
    public int value;

    public void f() {
        value = 100;
        System.out.println("FatherClass.value=" + value);
    }
}

class ChildClass extends FatherClass {
    public int value;//

    public void f() {
        super.f();
        value = 200;
        System.out.println("ChildClass.value=" + value);
        System.out.println(value);
        System.out.println(super.value);
    }
}

public class TestInherit {
    public static void main(String[] args) {
        ChildClass cc = new ChildClass();
        cc.f();
    }
}

   java原生就提供了继承的方式,使用extends操作符, js里ECMA5才提供了一个inherit, 还是有较大的区别的, 如果子类和超类有同名的方法, 那么子类不会继承超类的方法, 但是可以通过super直接调用, 这又让我想起了john resig 写的js继承

 

  javascript的this,这个东东在js里面坑过很多人, 但是在java里面他就是指向new出来的实例, 永远不会变的:

public class ThisDemo0 {  
    int number;
    ThisDemo0 increment(){
         number++;
         return this;
    }  
    private void print(){
         System.out.println("number="+number);
    }
    public static void main(String[] args) {
        ThisDemo0 tt=new ThisDemo0();
         tt.increment().increment().increment().print();
    }
}

 

以及:

public class ThisDemo1 {  
    String name;
    int age;
    public ThisDemo1(String name,int age){
        this.age = age;
        this.name = "Mick";
    }     
    private void print(){
         System.out.println("last-name="+this.name);
         System.out.println("last-age="+this.age);
    }
    public static void main(String[] args) {
       ThisDemo1 tt=new ThisDemo1("",0);
       tt.print();
    }
    //this 关键字是类内部当中对自己的一个引用,可以方便类中方法访问自己的属性;

}

 

  

  去年我还问钟毅, 构造函数中的抽象是什么意思, 反正最后还是迷迷糊糊的没弄懂, 抽象就是抽出对象之间相同的东西, 比如鸡和鸭,他们俩都是动物是吧, 那么我们就可以定义一个叫做动物的抽象类,abstract 就是抽象的意思;

abstract class Animal {
    private String name;

    Animal(String name) {
        this.name = name;
    }

    /*
     * public void enjoy(){ System.out.println("叫声......"); }
     */
  //这边又抽象了一个叫做enjoy的方法
public abstract void enjoy(); } class Dog extends Animal { private String furColor; Dog(String n, String c) { super(n); furColor = c; } public void enjoy() { System.out.println("dog shut......"); } } class Bird extends Animal { Bird() { super("bird"); } public void enjoy() { System.out.println("bird shut......"); } } class Lady { //在头部就定义了所有有关这个函数的变量和作用域访问权限; String name; private Animal pet; Lady(String name, Animal pet) { this.name = name; this.pet = pet; } public void myPetEnjoy() { pet.enjoy(); } } public class Test2 { public static void main(String args[]) { Dog d = new Dog("dogname", "black"); Bird b = new Bird(); // Lady l1 = new Lady("l1",c); Lady l2 = new Lady("l2", d); Lady l3 = new Lady("l3", b); // l1.myPetEnjoy(); l2.myPetEnjoy(); l3.myPetEnjoy(); System.out.println( l2.name ); } }

 

 

   java中定义一个构造函数的静态属性是在修饰符(public,abstract, protected,private)后面加上static, 表明了当前的方法或者属性是构造函数上面的方法或者属性, js中直接为构造函数加就好了,也有区别哦( •̀ ω •́ )y

public class Cat {
    private static int sid = 0;
    private String name;
    int id;

    Cat(String name) {
        this.name = name;
        id = sid;
    }

    public void info () {
        System.out.println("my name is" + name + "; id is " + sid);
    }

    public static void main( String arg[]){
        Cat.sid = 100;
        Cat mimi = new Cat("mimi");

        mimi.info();
        
        Cat.sid = 1600;
        Cat pipi = new Cat("pipi");

        pipi.info();
    }
}

 

   利用java去读取文件内容, import了 java.io下的所有类, 在这个类里面可以自己定义FileInputStrem类型的 变量, 利用try {} catch(){} finally{} 捕获异常, 因为文件可能会不存在哦:

import java.io.*;

public class TestExr {
    public static void main(String[] args) {
        FileInputStream in = null;
        try {
            in = new FileInputStream("TestExr.java");
            int b;
            b = in.read();
            while (b != -1) {
                System.out.print((char) b);
                b = in.read();
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            /*
             * } catch (FileNotFoundException e) { e.printStackTrace();
             */
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

  JAVA中的线程, 同步的线程:

public class TestSync implements Runnable {
    Timer timer = new Timer();

    public static void main(String[] args) {
        TestSync test = new TestSync();
        Thread t1 = new Thread(test);
        Thread t2 = new Thread(test);
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();
    }

    public void run() {
        timer.add(Thread.currentThread().getName());
    }
}

class Timer {
    private static int num = 0;

    public synchronized void add(String name) {
        num++;
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
        }
        System.out.println(name + ",you are" + num + " Thread ");        
    }
}

 

  implements是指继承 Runable, 并重写了超类的 run方法;

public class ThreadFromRunnable implements Runnable {  
  
    //static int count = 10;  
    public void run() {  
        int count = 10;  
        System.out.println("\t#"+Thread.currentThread().getName()+" got count from " + count);  
        while(count > 0)  
        {  
            System.out.println("#"+Thread.currentThread().getName()+" : "+ count--);  
        }  
        System.out.println("#"+Thread.currentThread().getName()+" : exiting "+ count--);  
    }
    
    public static void main(String[] args)  
    {  
        ThreadFromRunnable tr = new ThreadFromRunnable();  
        Thread thread = new Thread(tr);  
        Thread thread2 = new Thread(tr);  
          
        thread.start();  
        thread2.start();  
    }  
  
}  

 

   也可以通过extends继承线程:

public class ThreadExtendsThread extends Thread {
    public void run()  
    {  
        int count =10;  
        System.out.println("\t#"+Thread.currentThread().getName()+" got count from " + count);  
        while(count > 0)  
        {  
            System.out.println("{1}"+this.getName()+" : "+count--);  
        }  
        System.out.println("{1}"+this.getName()+" : existing count=" + count);  
    }  
      
    public static void main(String[] args)  
    {  
        ThreadExtendsThread thread = new ThreadExtendsThread();  
        ThreadExtendsThread thread2 = new ThreadExtendsThread();  
        thread.start();  
        thread2.start();  
    }  
}  

 

   从100到200中找出所有的素数, 我才知道素数是什么东东, 素数也叫做质数, 素数只能够被自己和1整除;

public class Prime{
    public static  int count = 0;
    public static void main ( String[] args) {
        for( int i = 100; i < 200 ; i++) {
            boolean f = true;
            for( int j = 2 ; j < i; j++) {
                if( i%j == 0) {
                    f = false;
                };
            };
            if( f ) {
                System.out.println("this number is" + i);
            }
        }
    }
}

 

   判断学生的考试得分的级别(A,B,C);

import java.util.Scanner;

public class ChaXun {
    static int grade;
    public static void main ( String[] args) {
        Scanner str = new Scanner( System.in );
        //等待用户输入;
        int s = str.nextInt();
        System.out.println( "this is you input number : " + s );
        if( grade == 1) {
            System.out.println("A");
        }else if( grade == 2) {
            System.out.println("B");
        }else{
            System.out.println("C");
        };
    };
    public int compare (int s) {
        return s > 90 ? 1 : s > 60 ? 2 : 3;
    };
};

 

  java中的类型好多啊, 搞得我要崩溃了:

import java.util.Scanner;

public class DiGui {

    public static void main (String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        DiGui str = new DiGui();
        System.out.println( str.recursion(n) );
    }

    public long recursion ( int n) {
        int result = 0;
        if( n == 1) {
            result = 1;
        }else if(n > 1){
            int temp = recursion( n-- );
            result = n * temp;
        };
        return result;
    }
}