JAVA Learing

The JAVA API(Built-in Packages)

1.The Java API is a library of prewritten classes, that are free to use, included in the Java Development Environment.

2.The library contains components for managing input, database programming, and much much more.

The complete list can be found at Oracles website:https://docs.oracle.com/javase/8/docs/api/

3.The library is divided into packages and classes. Meaning you can either import a single class (along with its methods and attributes), or a whole package that contain all the classes that belong to the specified package.

4.To use a class or a package from the library, you need to use the import keyword:

import package.name.Class;   // Import a single class
import package.name.*;   // Import the whole package

-d <目录> 指定放置生成的类文件的位置

Inner Class

public class Out {
    int x=10;
    public void out(){
        System.out.println("OUt work!");
    }
    public class In{
        int y=20;
        public void in(){
            System.out.println("In work!");
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Out myout = new Out();
        Out.In myin = myout.new In();
        myout.out();
        myin.in();
        System.out.println(myout.x + myin.y);
    }
}

运行结果:

OUt work!
In work!
30

在继承关系下,关于一次性创建(new)多个类(感觉上像数组),并且结合使用增强for循环进行遍历

public abstract class Animal {
    public abstract void Cry();
}
关于abstract修饰词的使用
强调了在父类中先定义(不写任何内容),但在子类中必须Override此方法
public class Cat extends Animal{
    public void Cry(){
        System.out.println("喵喵喵");
    }
}
public class Dog extends Animal{
    public void Cry(){
        System.out.println("汪汪汪");
    }
}
public class Main {
    public static void main(String[] args) {
        Animal[] animals = new Animal[2];
        animals[0] = new Dog();
        animals[1] = new Cat();
        for (Animal animal : animals) {
            animal.Cry();s
        }
    }
}
增强fo循环:
    可以简化数组和集合的遍历操作,animal是一个临时变量,用于存放数组中的元素,每次循环都会将数组中的下一个元素赋值给它,直到遍历完整个数组为止。
for (int i = 0; i < animals.length; i++) {
    animals[i].cry();
}
    这样写也可以达到同样的效果,但是相对来说代码会比较繁琐,特别是当数组中元素较多时。增强for循环的语法结构比较简单,使用起来也比较方便,所以在实际开发中更为常见。

interface

interface Animal{
    public void sleep();
    public void eat();
}
interface Zookeeper {
    public void feed();
}
class Cat implements Animal ,Zookeeper{
    public void sleep(){
        System.out.println("小猫睡觉");
    }
    public void eat(){
        System.out.println("小猫吃鱼");
    }
    public void feed(){
        System.out.println("喂小猫鱼");
    }
}
继承只能单继承,而可以实现多个接口
public class Main {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.sleep();cat.eat();cat.feed();
    }
}

运行结果:

小猫睡觉
小猫吃鱼
喂小猫鱼

Enum(枚举)的使用

public enum Level {
    low,
    medium,
    high
}
public class Main {
    public static void main(String[] args) {
        Level mylevel = Level.high;
        switch(mylevel){
            case low:
                System.out.println("Low level");
                break;
            case medium:
                System.out.println("Medium level");
                break;
            case high:
                System.out.println("High level");
                break;
        }

        //The enum type has a values() method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum:
        for(Level Mylevel : Level.values()){
            System.out.println(Mylevel);
        }
    }
}

computational results:

High level
low
medium
high
n enum:
for(Level Mylevel : Level.values()){
System.out.println(Mylevel);
}
}
}


>computational results:
>
>High level
>low
>medium
>high

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值