初印象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class HelloWorld {
public static void main(String[] args) { System.out.println("Hello, World!");
HelloWorld hw = new HelloWorld(); hw.sayHi("Alice"); }
public void sayHi(String name) { System.out.println("Hi, " + name + "!"); } }
|
java中所有的代码都要写在class里,同时public class只能在一个文件中出现一次,文件名要与public class的名字一致.
- 一个文件可以有多个class,也可以没有public class.
- main函数可以不写在public class里,但必须写成以下形式:
public static void main(String[] args) {//}
Overview
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
public class OOPDemo { public static void main(String[] args) { Dog dog = new Dog("Buddy", 3); Cat cat = new Cat("Kitty", 2);
Animal[] animals = {dog, cat}; for (Animal a : animals) { a.makeSound(); System.out.println(a.getInfo()); }
IFlyable bird = new Bird("Tweety", 1); bird.fly(); } }
abstract class Animal { protected String name; protected int age;
public Animal(String name, int age) { this.name = name; this.age = age; }
public abstract void makeSound();
public String getInfo() { return name + " is " + age + " years old"; } }
class Dog extends Animal { public Dog(String name, int age) { super(name, age); }
@Override public void makeSound() { System.out.println("Woof Woof!"); } }
class Cat extends Animal { public Cat(String name, int age) { super(name, age); }
@Override public void makeSound() { System.out.println("Meow!"); } }
interface IFlyable { void fly(); }
class Bird implements IFlyable { private String name; private int age;
public Bird(String name, int age) { this.name = name; this.age = age; }
@Override public void fly() { System.out.println(name + " is flying!"); } }
|
多文件处理
package
1 2 3 4 5 6 7 8 9 10
| package com.example;
import com.example.utils.StringUtils;
public class MainApp { public static void main(String[] args) { StringUtils.shout("hello"); } }
|
- package语句必须写在java文件的第一行,表示这个文件属于这个包,其他文件才可以通过包来访问到这个文件
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.example;
import com.example.animals.Dog; import com.example.animals.Cat;
import com.example.utils.StringUtils;
public class MainApp { public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat();
dog.makeSound(); cat.makeSound();
StringUtils.shout("hello world"); } }
|
1 2 3 4 5 6 7 8 9 10
| package com.example.animals;
public abstract class Animal { protected String name; protected int age;
public abstract void makeSound(); }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.example.animals;
import com.example.animals.Animal;
public class Dog extends Animal { @Override public void makeSound() { } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.example.animals;
import com.example.animals.Animal;
public class Cat extends Animal { @Override public void makeSound() { } }
|
1 2 3 4 5 6 7 8 9 10
| package com.example.utils;
public class StringUtils { public static String shout(String input) { return null; } }
|
interface and implements
- java不允许多重继承,故只能继承自一个父类
class A extends B,故有了interface这一公共类,可以让一个类实现多个接口class A implements X, Y, Z
内存管理
注意到java通过new来实例化对象,而没有用delete之类的关键字进行回收,是因为java也是自动管理内存的语言.
java的垃圾回收机制
java没有采用引用计数,而是采用对象图遍历的方法来回收无法到达的对象,从而确保一对互相引用却没有真正使用的对象也能被回收
基础语法
变量
变量类型
Number是 Integer、Long、Float、Double、Byte 和 Short 的共同父类,很好的解决了cpp中如果不使用模板就需要针对整数,浮点数进行不同处理的问题
- 定义变量的时候,如果加上final修饰符,这个变量就变成了常量:
1 2 3 4
| final double PI = 3.14; double r = 5.0; double area = PI * r * r; PI = 300;
|
- 值得注意的是java中的数组创建也用new,但String类型却不用new
1 2 3 4 5 6 7 8 9 10 11 12
| public class Main { public static void main(String[] args) { int[] ns = new int[5]; ns[0] = 68; ns[1] = 79; ns[2] = 91; ns[3] = 85; ns[4] = 62; } }
|
OOP
多态与继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Main { public static void main(String[] args) { } }
class Person { public void run() {} }
public class Student extends Person { @Override public void run(String s) {} }
|
- 不过
@override不是必需的,仅仅是用来给编译器检查的,保证你不会写成不同的函数
抽象类与接口
abstract class相当于一个必须要派生类override对应抽象方法的基类.
1 2 3 4
| public interface Person { void run(); String getName(); }
|
上述代码相当于以下代码
1 2 3 4
| public abstract Person{ abstract void run(); abstract String getName(); }
|
- 派生类继承这个基类时需要使用
implements关键字而不是extends
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Student implements Person { private String name;
public Student(String name) { this.name = name; }
@Override public void run() { System.out.println(this.name + " run"); }
@Override public String getName() { return this.name; } }
|
静态字段与静态方法(3/3)
1 2 3 4 5 6
| class Person { public String name; public int age; public static int number; }
|
- 这里的number相当于Person类自身带有的变量,由所有实例共享,当然子类也会共享
- 可以用类名来直接访问静态字段
Person.number = 99; System.out.println(Person.number);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Main { public static void main(String[] args) { Person.setNumber(99); System.out.println(Person.number); } }
class Person { public static int number;
public static void setNumber(int value) { number = value; } }
|
- 静态方法与静态字段一样,属于这个类,可以被所有实例调用,但只能访问这个类的静态字段
1 2 3 4 5
| public interface Person { int MALE = 1; int FEMALE = 2; }
|
- 有趣的是interface中的字段只能是静态常量,不允许子类来修改
进阶语法
什么是JavaSE,JavaEE
Java有两个主要版本,一个是(Standard Edition),一个是(Enterprise Edition),实际上上JavaSE相当于基础语法,JavaSE相当于加上了很多API和规范