static
static属性及方法
package com. oop. demo07;
public class Student {
public static int age;
public double score;
public void run ( ) {
go ( ) ;
}
public static void go ( ) {
}
public static void main ( String[ ] args) {
Student s1 = new Student ( ) ;
System. out. println ( Student. age) ;
System. out. println ( s1. score) ;
System. out. println ( s1. age) ;
Student. go ( ) ;
s1. run ( ) ;
}
}
static代码块
package com. oop. demo07;
public class Person {
{
System. out. println ( "匿名代码块" ) ;
}
static {
System. out. println ( "静态匿名代码块" ) ;
}
public Person ( ) {
System. out. println ( "无参构造器" ) ;
}
public static void main ( String[ ] args) {
Person person = new Person ( ) ;
Person person1 = new Person ( ) ;
}
}
static导入包
package com. oop. demo07;
import static java. lang. Math. random;
import static java. lang. Math. PI;
public class Test {
public static void main ( String[ ] args) {
System. out. println ( random ( ) ) ;
System. out. println ( PI) ;
}
}
抽象类
是使用关键字abstract 来修饰,修饰类就是抽象类,修饰方法就是抽象方法 抽象类中可以没有抽象方法,但抽象方法必须存在于抽象类中 抽象类不能使用new关键字来创建对象,它是由子类来继承的 抽象方法只有方法的声明,没有方法是实现,它是用来由子类去实现的 子类继承抽象类,就必须实现抽象类中没有实现的方法,除非该子类也为抽象类
package com. oop. demo08;
public abstract class Action {
public abstract void doSomething ( ) ;
public void run ( ) {
}
}
package com. oop. demo08;
public class A extends Action {
public A ( ) {
super ( ) ;
}
@Override
public void doSomething ( ) {
}
}
接口
接口就是规范,定义的是一组规则,是一个专业的约束 接口的本质是契约,就像法律一样,制定好了,大家都要去遵守 面向对象的精髓是对对象的抽象,最能体现这一点的就是接口 声明类的关键字为:class ,声明接口的关键字为:interface
package com. oop. demo09;
public interface UserService {
int age= 10 ;
void add ( String name) ;
void delete ( String name) ;
void update ( String name) ;
void query ( String name) ;
package com. oop. demo09;
public interface TimeServica {
void timer ( ) ;
}
package com. oop. demo09;
public class UserServicaImpl implements UserService , TimeServica {
@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 ( ) {
}
}
内部类
成员内部类
package com. oop. demo10;
public class Outer {
private int id= 1001 ;
private void run ( ) {
System. out. println ( "run" ) ;
}
public void out ( ) {
System. out. println ( "这是外部类的方法" ) ;
}
public class Inner {
public void in ( ) {
System. out. println ( "这是内部类的方法" ) ;
}
public void id ( ) {
System. out. println ( id) ;
run ( ) ;
}
}
}
package com. oop;
import com. oop. demo10. Outer;
public class Application {
public static void main ( String[ ] args) {
Outer outer = new Outer ( ) ;
outer. out ( ) ;
Outer. Inner inner = outer. new Inner ( ) ;
inner. in ( ) ;
inner. id ( ) ;
}
}
局部内部类
package com. oop. demo10;
public class Test1 {
public void outer ( ) {
class Inner {
public void in ( ) {
System. out. println ( "这是局部内部类" ) ;
}
}
new Inner ( ) . in ( ) ;
}
}
package com. oop;
import com. oop. demo10. Outer;
import com. oop. demo10. Test1;
public class Application {
public static void main ( String[ ] args) {
Test1 test1 = new Test1 ( ) ;
test1. outer ( ) ;
}
}
匿名内部类
package com. oop. demo10;
public class Test2 {
public static void main ( String[ ] args) {
new Apple ( ) . eat ( ) ;
}
}
class Apple {
public void eat ( ) {
System. out. println ( "1" ) ;
}
}