java 类和对象 概念

  1. 面向对象程序设计

OOP:将数据和对数据的操作行为放在一起

2、类与对象

public class ObjectTest {
    public static void main(String args[]) {
        Box box1=new Box();
        System.out.print(box1.getWidth());
    }
}
class Box{
    //创建一个box类。包含成员变量和方法
    //成员变量
    int width;
    int height;
    private int depth;
    String getWidth() {
        return String.valueOf(width);
    }
}

  3、类成员与作用域

域、方法、构造方法:构造方法使用new的时候为该对象分配内存,并调用构造方法来初始化对象

[类的修饰符] class 类名 [extends 父类]

 

类的作用域:public、package(缺省)

 区分类的作用域、成员变量的作用域、方法中变量的作用域

 

域:成员变量,public、protected、缺省值、private、static、final

  public 可以在类外被使用

  protected 在同一包或子类中使用

  缺省: 在同一包中被使用

  private:只能在类中访问

  

  4、构造方法与重载

1构造方法与类名同名

2构造方法无返回类型,参数可重载

public class OverloadTest {
    public static void main(String args[]) {
        boxtest box1=new boxtest();
        boxtest box2=new boxtest(true);
        boxtest box3=new boxtest(45);
    }
}
class boxtest {
    boxtest(){
        System.out.print("no parameter");
    }
    boxtest(int h){
        System.out.print(h);
    }
    boxtest(boolean b){
        System.out.print(b);
    }
}

结果:no parametertrue45

   5、this

被调用的方法的当前对象的引用,仅能出现在类中的方法体中。

  //this 指代box2

public class ThisTest {
    public static void main(String args[]) {
        lovelybox box2=new lovelybox(5,6,7);
        System.out.print(box2);
    }
}

class lovelybox{
    private int fir;
    private int sec;
    private int third;
    lovelybox(int fir,int sec,int third) {
        this.fir=fir;
        this.sec=sec;
        this.third=third;
    }
    public String toString() {
        return String.valueOf(fir)+String.valueOf(sec)+String.valueOf(third);
    }
}

  6、set 和get

set变量和get变量的方法对私有变量进行修改,可对参数进行检测

public class SetGetTest {
    public static void main(String b[]) {
        animal katty = new animal();
        System.out.println(katty.getKind());
        System.out.println(katty.getSize());
        katty.setKind("pussy");
        katty.setKind("cat");
        System.out.println(katty.getKind());
    }
}
class animal{
    private int size=10;
    private String kind="dog";
    public void setKind(String changeKind) {
        if(changeKind!="cat") {
            System.out.println("It is not a cat");
        }
        else {
            kind=changeKind;
            System.out.println("animal is a"+kind);
        }
    }
    public String getKind() {
        return kind;
    }
    public int getSize() {
        return size;
    }
}

  7、static 方法和域

static 变量\方法 (类的变量、方法)-----是指成员变量是类和它的所有对象共同拥有的

非静态--------各个对象分别有自己的同名变量、方法

 

使用格式:

  类名.静态变量名;类名.静态变量方法;

静态方法的成员变量也是静态的

  8、Main方法

public static void main(String args[]){}

main()方法是Java应用程序的入口方法。

运行 java  com.Helloworldl.Main

当运行一个类时,JVM调用main()方法。

  9、实例初始化器、静态初始化器

实例初始化器---在实例化一个对象之前执行,无方法名。用{}包含在内

静态初始化器---在jvm加载类定义的时候执行,只执行一次。

public class InitializerTest {
    public static void main(String args[]) {
        testIn test1;
        System.out.println("hi");
        test1=new testIn();
        testIn test2;
        test2=new testIn();
    }
}
class testIn{
    private static int times;
    {
        times+=1;
        System.out.println("Instance Initializer has been runned for "+String.valueOf(times)+" times");
    }
    static {
        times+=1;
        System.out.println("static Initializer has been runned for"+String.valueOf(times)+" times");
    }
    testIn(){
        System.out.println("constructor");
    }
}

   10、垃圾收集

1、当对象不再被使用时,会被java清除掉。显式的方法:System.gc

garbage collector :Java垃圾收集器

当对象=null;时对象不再被引用,可被清除

2、可定义finalize()方法,protected void finalize(){}

当对象被清除时:自动运行该方法

 

 

11、java常用包列表

java.lang:基本语言类,自动导入

java.io

java.util  实用的数据类型类

java.awt\java.applet

java.net

 

posted @ 2020-10-12 01:50  浪里薯条  阅读(133)  评论(0)    收藏  举报