//Student.java public class Student { public static void sayHi(){ System.out.println("Hello World!"); } } //main.java public class main { public static void main(String[] args) { Student.sayHi(); } }
//Student.java public class Student { public void sayHi(){ System.out.println("Hello World!"); } } //main.java public class main { public static void main(String[] args) { Student student = new Student(); student.sayHi(); } }
//可以直接调用 public static void a(){ b(); } public static void b(){} //可以直接调用 public void a(){ b(); } public void b(){} //不可以调用 //方法 a 由 static 修饰,与类一起加载 //方法 b 无 static 修饰符,只有实例化之后才会在内存中加载,因此此种调用方式错误 public static void a(){ b(); } public void b(){}
public class main { public static void main(String[] args) { int num = 5; System.out.println(num); //num = 5 change(num); System.out.println(num); //num = 5 } public static void change(int num){ num = 100; } }
public class main { public static void main(String[] args) { Person person = new Person(); System.out.println(person.name); //null main.change(person); System.out.println(person.name); //Bob } public static void change(Person person){ person.name = "Bob"; } } class Person { String name; }
// new 的本质,就是在调用构造器 public class Person { String name; //无参构造 public Person(){ this.name = "Bob"; } //有参构造 public Person(String name){ this.name = name; } }
public class main { public static void main(String[] args) { Pet dog = new Pet(); dog.name = "旺财"; dog.age = 3; dog.shout(); Pet cat = new Pet(); } } public class Pet { public String name; public int age; public void shout(){ System.out.println("Shout!"); } }
封装
public class Pet { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if (age < -1 || age > 120){ this.age = 3; }else{ this.age = age; } } } public class main { public static void main(String[] args) { Pet dog = new Pet(); dog.setAge(500); System.out.println("age = " + dog.getAge()); //age = 3 } }
get / set
继承:本质是对一批类的抽象,使用关键字 extends
public class Pet { private String name = "Bob"; protected int age = 5; int money = 100; public void shout(){ System.out.println("Hi"); } } public class Cat extends Pet {} public class main { public static void main(String[] args) { Cat cat = new Cat(); cat.shout(); //Hi System.out.println(cat.name); //Error System.out.println(cat.age); //5 System.out.println(cat.money); //100 } }
public、protected、default
修饰的所有属性和方法,private
修饰的则不会被继承Java
中所有的类,都默认继承 Object
类Java
中的类都是单继承,没有多继承public class Pet { public String name = "Pet"; public void shout(){ System.out.println("Father"); } } public class Cat extends Pet { public String name = "Cat"; public void print(){ System.out.println("Son"); System.out.println(super.name); super.shout(); } } public class main { public static void main(String[] args) { Cat cat = new Cat(); cat.print(); // Son Pet Father } }
super
注意点
super
调用父类的构造方法时,必须出现在子类构造方法的第一行super
必须只能出现在子类的方法或者构造方法中super
和 this
不能同时调用构造方法super VS this
this
:本身调用者这个对象super
:代表父类的引用this
:有无继承均可使用super
:只能在继承的条件下使用this
:本类的构造方法super
:父类的构造方法方法重写
public > protected > default > private
//使用 static 关键字:方法的调用只和左边的类型有关 public class B { public static void test(){ System.out.println("B ==> test()"); } } public class A extends B{ public static void test(){ System.out.println("A ==> test()"); } } public class main { public static void main(String[] args) { A a = new A(); a.test(); //A ==> test() B b = new A(); b.test(); //B ==> test() } } //方法重写:重写的定义只针对无 static 修饰的方法 public class B { public static void test(){ System.out.println("B ==> test()"); } } public class A extends B{ //对父类的 test() 方法进行了重写 @Override public static void test(){ System.out.println("A ==> test()"); } } public class main { public static void main(String[] args) { A a = new A(); a.test(); //A ==> test() B b = new A(); b.test(); //A ==> test() } }
多态
public class Person { public void run(){ System.out.println("Person ==> run()"); } } public class Student extends Person{ @Override public void run() { System.out.println("Student ==> run()"); } public void eat(){ System.out.println("Student ==> eat()"); } } public class main { public static void main(String[] args) { Student s1 = new Student(); Person s2 = new Student(); Object s3 = new Student(); s1.run(); //Student ==> run() s1.eat(); //Student ==> eat() ((Student) s2).eat(); //Student ==> eat() } }
ClassCastException
public class Person { { System.out.println("匿名代码块"); } static { System.out.println("静态代码块"); } Person(){ System.out.println("构造方法"); } public static void main(String[] args) { Person person1 = new Person(); System.out.println("-----------"); Person person2 = new Person(); } } /* 静态代码块 匿名代码块 构造方法 --------- 匿名代码块 构造方法 */
abstract
修饰new
抽象类,只能靠子类去实现public abstract class Person { public abstract void doSomething(); } public class Student extends Person{ @Override public void doSomething() { } }
public abstract class Person { public abstract void doSomething(); Person(){ System.out.println("构造方法"); } } public class Student extends Person{ @Override public void doSomething() { System.out.println("doSomeThing()"); } } public class main { public static void main(String[] args) { Student student = new Student(); } } //构造方法
interface
,只有规范,自己无法写方法,实现约束和具体步骤的分离,更加规范public abstract
修饰public interface UserAction { void run(); }
implements
public interface UserAction { void add(String name); void delete(String name); void update(String name); void query(String name); } public interface UserTime { void timer(String name, int age); } public class UserActionImpl implements UserAction, UserTime{ @Override public void add(String name) { } @Override public void delete(String name) { } @Override public void update(String name) { } @Override public void query(String name) { } @Override public void timer(String name, int age) { } }
public class Outer { private final int id = 10; public void printOut(){ System.out.println("外部类的方法"); } class Inter { public void printIn(){ System.out.println("内部类方法"); } public void getOutId(){ System.out.println(id); } } } public class main { public static void main(String[] args) { Outer outer = new Outer(); Outer.Inter inter = outer.new Inter(); inter.getOutId(); //10 } }
public class Outer { public void method(){ class test{ } } }
public class main { public static void main(String[] args) { new Apple().eat(); } } class Apple{ public void eat(){ } }