Java 杂谈Java

Java 基础知识点学习(一)

2019-04-28  本文已影响1人  smallmartial

title: Java 基础知识点学习(一)
date: 2019-04-28 13:27:00
tags: java
categories: java


1. Java面向对象基本概念 - 引用

2.如何区分类和对象

3.递归调用

斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368。

公式: f1= 1,f2=1. fn=f(n-1)+f(n-2)

这个数列从第3项开始,每一项都等于前两项之和。

   public static int testFibonacci(int n) {
        if (n == 1 || n == 2) {
            return 1;
        }else {
            return test3(n-1) + test3(n-2);
        }
    }

递归调用示例图:

1556332700135

4.方法重载

方法的重载是指一个类中可以定义相同的名字,但是参数不同的多个方法。调用时,会根据不同的参数选择对应的方法。

示例:

public class Demo{
    void info(){
        System.out.println("hello world");
    }
    void info(String t){
         System.out.println("hello"+ t);

    }
    
    public static void main(String args[]){
        Demo d = new Demo();
        d.info();
        d.info("smallmartial");
    }
}

5.this关键字

public class Leaf{
    int i = 0;
    Leaf(int i){
        this.i = i ;
    }
    Leaf increament(){
        i++;
        return this;
    }
    void print(){
       System.out.println("i"+ i);
    }
    
    public static void main(String args[]){
        Leaf leaf = new Leaf(100);
        leaf.increament().increament().print();
    }    
}
//result: 102

6.static关键字

7.类的继承

public class Animal { 
    private String name;  
    private int id; 
    public Animal(String myName, int myid) { 
        name = myName; 
        id = myid;
    } 
    public void eat(){ 
        System.out.println(name+"正在吃"); 
    }
    public void sleep(){
        System.out.println(name+"正在睡");
    }
    public void introduction() { 
        System.out.println("大家好!我是"         + id + "号" + name + "."); 
    } 
}

子类

public class Penguin extends Animal { 
    public Penguin(String myName, int myid) { 
        super(myName, myid); 
    } 
}

public class Mouse extends Animal { 
    public Mouse(String myName, int myid) { 
        super(myName, myid); 
    } 
}

8.访问控制

1556367386217
上一篇 下一篇

猜你喜欢

热点阅读