面向对象

2020-04-19  本文已影响0人  雪上霜
image.png image.png image.png
public  class User{
    int id;
    String user;
}
public class UserTest{
    public static void main(String[] args){
        User u1 = new User();
        u1.id = 111;
        u1.user = "张三";
        
        System.out.println(u1.id);
        System.out.println(u1.user);
    }
}
image.png
public class Addr{
    String city;
    String jd;
}
public class User{
    String username;
    int id;
    Addr addr;
}
public class Test{
    public static void main(String[] args){
        User u = new User();
        System.out.println(u.id);
        System.out.println(u.username);
        System.out.println(u.addr);
        
        Addr c  = new Addr();
        c.city = "beij";
        c.jd = "daxing";
        
        u.addr = c;
        
        System.out.println(u.addr.city);
    }
}
image.png
public class T{
    A a;
    
    public static void main(String[] args){
        D d = new D();
        C c = new C();
        B b = new B();
        A a = new A();
        T t = new T();
        
        //以下赋值不写会出现空指针异常。
        c.d = d;
        b.c = c;
        a.b = b;
        t.a = a;
        
        //通过t访问d中的i
        System.out.println(t.a.b.c.d.i);
    }
}

class A{
    B b;
}
class B{
    C c;
}

class C{
    D d;
}
class D{
    int i;
}
public class NullPointerTest{
    public static void main(String[] args){
        Customer c = new Customer();
        
        System.out.println(c.id);
        
        c.id = 9876;
        System.out.println(c.id);
        
        c = null;//编译器只检查语法。语法没错。
        System.out.println(c.id);
    }
}

class Customer{
    int id;
}
image.png
  public class Test{
    public static void main(String[] args){
        int i = 10;
        add(i);
        System.out.println("main-->"+i);
    }
    public static void add(int i){
        i++;
        System.out.println("add->"+i);
    }
  }
360截图20200417112338296.jpg
public class Test{
    public static void main(String[] args){
        Person p = new Person();
        p.age = 10;
        add(p);
        System.out.println("main->"+p.age);
    }
    public static void add(Person p){
        p.age++
        System.out.println("add->"+p.age);
    }
}

class Person{
    int age;
}
image.png
public class MethodTest{
    public static void main(String[] args){
        //调用static的调用方法
        MethodTest.doSome();
        doSome();
        
        //不带static的调用方法
        MethocTest mt = new MethodTest();
        mt.doOther();
    }
    //带static方法
    public static void doSome(){
        System.out.println("wss");
    }
    
    //无static方法,实例方法,必须通过对象调用
    public void doOther(){
        
    }
}
//空指针异常本质是空引用访问实例相关数据。
public class NullPointerTest{
    public static void main(String[] args){
        User u = new User();
        System.out.println(u.id);
        u.doSome();
        
        u = null;
        System.out.println(u.id);//需要对象存在,空指针异常
        u.doSome();//一个实例方法调用必须有对象的存在,所以空指针异常。
    }
}

class User{
    int id;
    public void doSome(){
        System.out.println("dosome");
    }
    public void exam(){
        
    }
}
public class PersonTest{
    public static void main(String[] args){
        Person p = new Person();
        int nianLing = p1.getAge();
        System.out.println(nianLing);
        
        
        p1.setAge(100);
        System.out.println(p1.getAge());
        
        p1.setAge(-100);
        System.out.println(p1.getAge());
    }
}
上一篇 下一篇

猜你喜欢

热点阅读