类和对象的总结

一.类的来源和定义

1.概念:类是用来对一个实体(对象)来进行描述的,相当于一个模板,通过整个模板可以实例化多个对象.所以,一个类可以实例化多个对象. 

2.代码表示

class 大驼峰{

1.对象的属性(成员变量) //方法外部,类内部

2.方法(成员方法)

}

二.类定义完成,如何产生对象?如何实例化一个对象?

类(类型) 对象名=new 类();
例如:
Dog  dog =new Dog();

三.已经实例化一个对象,如何给对象里面成员变量赋值?

1.定义成员变量时直接赋值(就地初始化)

class Dog{
String name="小黑";
int age=10;
}

 2.不初始化,直接是默认值

引用类型---null  int----0   float-----0.0f   boolean------false  char----"\u000"

3.不在同一个类,通过对对象的引用进行赋值

class Dog{
    //成员变量,类的内部,方法的外部=
    public String color;
    public String name;
    public void barks(){
        System.out.println(name+":汪汪叫");
    }
    public void wag(){
        System.out.println(name+":摇尾巴");
    }
    public void show(){
        System.out.println("姓名:"+name+"颜色:"+color);
    }

}
public class Test {
    public static void main(String[] args) {
        //new关键字来实例化
        Dog dog=new Dog();
        //通过对象的引用来访问里面的成员变量
        System.out.println(dog.color);
        System.out.println(dog.name);
        dog.wag();
        dog.barks();
        //赋值
        dog.name="初一";
        dog.color="红色";
        dog.wag();
        dog.barks();
        dog.show();
    }

4.通过构造方法进行初始化

class Dog{
    //成员变量,类的内部,方法的外部=
    public String color;
    public String name;
    public void barks(){
        System.out.println(name+":汪汪叫");
    }
    public void wag(){
        System.out.println(name+":摇尾巴");
    }
    public Dog(String color,String name){
        this.color=color;
        this.name=name;
    }

四.关于this的知识点 

1.定义:this是引用,代表对当前对象进行引用.

2.this.成员变量 ->对成员变量进行赋值

3.this.方法--->调用当前对象的成员方法

4.this() 必须在构造方法里面,而且必须在构造方法第一行,调用不能形成环的调用

class People{
    public String name;
    public int age;
    public String sex;
    public People(){
        this("小加",18,"女");
    }

    public People(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

五.关于构造方法

class People{
    public String name;
    public int age;
    public String sex;
    public People(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

构造方法:方法名与类名一致,无返回值.整个过程只调用一次,在类实例化对象时,当没有写构造方法时,编译器不会报错,会提供一个不带参数的构造方法,如果我们自己写了一个构造方法,那么编译器将不会给我们提供构造方法.

六.类中变量的分类

 1.普通成员变量/方法

通过对象引用来输出,并且赋值.

2.静态成员变量/方法

访问要通过类名.成员变量/方法来访问,可以不用new一个对象

在静态成员方法中,不能直接访问非静态成员变量,如果必须要访问,则要new一个对象,通过引用对象来进行访问.

//访问静态成员变量/方法
class People{
    public  String name;
    public int age;
    public String sex;
    static int grade;
    public static void eat(){

    }
}
public class Test {
    public static void main(String[] args) {
     People.grade=10;
    }
}

//要在静态成员方法中访问非静态变量
class People{
    public  String name;
    public static int age;
    public String sex;
    static int grade;
    public static void eat(){
        People people=new People();
        System.out.println(people.name+"吃饭");
    }
}

七.内部类分类

1.实例内部类

(1)实例内部类不能定义静态成员变量/成员方法  (但是如果给静态成员变量前面加final变成常量,则可以修饰)

(2)怎样实例化内部类?  外部类.内部类 内部类名=new 外部类().new 内部类()

class People{
    public  String name;
    public static int age;
   class Demo{
       public int grade;
       public String sex;
   }
}
public class Test {
    public static void main(String[] args) {
      People.Demo demo=new People().new Demo();
    }
}

 (3)如何访问实例内部类和外部类同名的成员变量?

class People{
    public  String name;
    public static int age;
    public int grade;
   class Demo{
       public int grade;
       public String sex;
       public void fuc(){
           System.out.println(this.grade);//访问内部类的成员变量
           System.out.println(People.this.grade);//访问外部类成员变量
       }
   }
}
public class Test {
    public static void main(String[] args) {
      People.Demo demo=new People().new Demo();
    }
}

2.静态内部类

(1)如何实例化静态内部类对象? 外部类.内部类 内部类对象=new 外部类.内部类()

public class Test {
    public static void main(String[] args) {
     // People.Demo demo=new People().new Demo();
      People.Demo demo2=new People.Demo();
    }

(2)不能在静态内部类中直接访问外部类的非静态成员,如果必须要访问,先在静态内部类方法中new一个外部类对象,然后对象名.属性就可以访问

class People{
    public  String name;
    public static int age;
    public int grade;
   static class Demo{
       public int grade;
       public String sex;
      public void fuc(){
          People people=new People();
          System.out.println(people.name+"吃饭");
      }
   }
}

3.匿名内部类

4.局部内部类

定义在方法内部,类不能被static .public 修饰,只可以在当前方法里面实例化对象

lass People{
    public  String name;
    public static int age;
    public int grade;
    public void fuc(){
        class People1{
            int a=1;
            int b=3;
            People1 people1=new People1();
        }
    }
}

八.代码块

1.普通代码块:定义在方法内部

2.实例代码块:方法外部,类内部

3.静态代码块:方法外部,类内部

class People{
    public  String name;
    public static int age;
    public int grade;
    static {
        System.out.println("静态代码块");
    }
    {
        System.out.println("实例代码块");
    }
    public void fuc(){
        System.out.println("本块代码块");
    }
}

当三个代码块都存在时,先执行静态代码块,在执行实例代码块

静态代码块最先被加载,而且只执行一次

如果有多个静态代码块时,看定义顺序进行

如果没有实例化对象,只会执行静态代码块内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值