定义类和创建对象

定义类和创建对象

Class

类是定义了相同类型对象的构造
在Java类中可以去使用变量去定义数据区域和方法去定义行为
此外,类提供了一种特殊的方法也就是构造方法,成为构造函数,
调用这些方法可以从类中构造对象
package demo03.oop01;

/**
 * @program: interview
 * @author: spring
 * @create: 2024-12-23
 */
class Circle {

    /**
     * data field
     */
    // The radius of the circle
    double radius = 1.0;

    /**
     * construct
     */
    // Construct a circle object
    Circle() {
        // no args
    }
    
    // Construct a circle object
    Circle(double newRadius) {
        // args
        radius = newRadius;
    }

    /**
     * method
     */
    double getArea() {
        return radius * radius * 3.14;
    }
}


定义类和创建对象的过程

package demo03.oop01;

/**
 * @program: interview
 * @author: spring
 * @create: 2024-12-23
 */
public class SimpleCircle {

    // data field
    double radius;

    // no args construct
    SimpleCircle() {

    }

    // args construct
    SimpleCircle(double radius) {
        this.radius = radius;
    }

    // return the area of this circle
    double getRadius() {
        return radius * radius * Math.PI;
    }

    // return the perimeter of the circle
    double getPerimeter() {
        return 2 * radius * Math.PI;
    }

    // Set a new radius for this circle 
    void setRadius(double radius) {
        this.radius = radius;
    }
}

package demo03.oop01;

/**
 * @program: interview
 * @author: spring
 * @create: 2024-12-23
 */
public class TestSimpleCircle {
    public static void main(String[] args) {

        SimpleCircle circle1 = new SimpleCircle();
        System.out.println(circle1.radius + "  " + circle1.getRadius());

        SimpleCircle circle2 = new SimpleCircle(25);
        System.out.println(circle2.radius + "  " + circle2.getRadius());


    }
}

another example
package demo03.oop01;

/**
 * @program: interview
 * @author: spring
 * @create: 2024-12-23
 */
public class TV {

    private int channel = 1;
    private int volumeLevel = 1;
    private boolean on = false;

    public TV() {

    }

    public TV(int channel, int volumeLevel, boolean on) {
        this.channel = channel;
        this.volumeLevel = volumeLevel;
        this.on = on;
    }

    public int getChannel() {
        return channel;
    }

    public void setChannel(int channel) {
        // using if else to check 
        this.channel = channel;
    }

    public int getVolumeLevel() {
        return volumeLevel;
    }

    public void setVolumeLevel(int volumeLevel) {
        // using if else to check 
        this.volumeLevel = volumeLevel;
    }

    public boolean isOn() {
        return on;
    }

    public void setOn(boolean on) {
        this.on = on;
    }

    public void turnOn() {
        on = true;
    }

    public void turnOff() {
        on = false;
    }
    
    public void channelUp(){
        if (on&&channel<120){
            channel++;
        }
    }
    
    public void  channelDown(){
        if (on&&channel>1){
            channel--;
        }
    }

}
package demo03.oop01;

/**
 * @program: interview
 * @author: spring
 * @create: 2024-12-23
 */
public class TestTv {

    public static void main(String[] args) {

        TV tv1 = new TV();
        tv1.turnOn();
        tv1.setChannel(30);
        tv1.setVolumeLevel(3);

        TV tv2 = new TV();
        tv2.turnOn();
        tv2.channelUp();
        tv2.channelDown();
        tv2.setVolumeLevel(50);

        System.out.println(tv1.channel + " " + tv1.volumeLevel);
        System.out.println(tv2.channel + " " + tv2.volumeLevel);
    }
}

Construct

// no args construc
Circle(){
    
}

// args construct
Circle(double radius){
    this.radius = radius;
}

构造函数是用来构造对象的一种特殊的方法

没有形参的构造函数叫做无参构造函数

A constructor with no parameters is referred to as a no-arg constructor

the three feature

  • 构造函数的名称必须和类名是相同的
  • 构造函数没有返回值-甚至没有void
  • 创建对象时使用new操作符调用构造函数,构造函数起到了初始化对象的作用
  • Constructor must be the same as the Class name
  • Constructor doesn't have the value of the return - even the void
  • create the object uses the opeator of the new to invoke the constructor , it play a role in the initalize the object
new ClassName();

// Example
new Circle();

new Circle(5.0);

默认构造函数 default constructor

A class may be defined without constructors . In this case, a no-arg constructor with an empty body is implicitly(implicitly) declared in the class. This construcor is called the default constuctor , is provided automatically only if no constructos are explicitly defined in the class

类在没有构造方法的时候,在这种情况下,JVM 会主动在在类中隐式的给我们添加上一个无参构造方法,这个无参构造方法不是我们手动的进行添加的,所以也叫做默认构造方法,仅存在类中没有显式的定义构造函数的时候,这个默认构造方法才会自己去进行添加

声明对象引用变量

Declaring OBject Reference Variables

要去引用对象的话,首先我们要把对象赋值给引用变量

To reference an object , assign the object to a reference variable

ClassName objRefVar
Circle myCirCle

Declare / create Object in a singel step

ClassName objRefVar = new ClassName();
new Circle // create the object
myCircle // assign object reference

  • reference the object' s data
objRefVar.data
myCircle.radius
  • invoke the object's method
objRefVar.method()
myCircle.getArea()

  • declare the refVar
  • Create the obj
  • assign the objRef to the refVar
  • change radius in youCircle

在面向对象之前所学习的所有的类,eg Math 这样的都是由static所修饰的静态类,是可以直接通过这个. dot运算符直接去进行调用的

但是我们现在所学的是没有加上static关键字,也就并非静态的

必须是要去通过对象去进行相关的调用的

Reference Data Field

数据字段可以是引用类型的

public class Student{
    String name;
    int age;
    boolean isScienceMajor;
    char gender;
}

如果引用类型的数据字段不引用任何的对象的话,则该数据字段将保存一个特殊的文字值

public class Person {
    // 引用类型字段
    String address;

    public static void main(String[] args) {
        Person person = new Person();
        
        // 在没有显式赋值的情况下,address字段默认值为null
        System.out.println(person.address);  // 输出 null
    }
}

数据字段的默认值

package demo03.oop01;

import demo02.test01.Student;

/**
 * @program: interview
 * @author: spring
 * @create: 2024-12-23
 */
public class Test {

    public static void main(String[] args) {


        Student student = new Student();
        System.out.println(student.getName());
        System.out.println(student.getAge());
        System.out.println(student.getGender());
    }
}
  • 对于数字来说,默认值是0
  • 布尔类型来说,默认是false
  • char字符型,\u0000
  • 然而Java中没有给方法中的局部变量赋默认值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值