java 14:对象与类,构造函数,访问对象

1 定义对象的类

Object-oriented programming (OOP) involves programming using objects. An object repre-
sents an entity in the real world that can be distinctly identified. For example, a student, a
desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique
identity, state, and behavior.
■ The state of an object (also known as its properties or attributes) is represented by
data fields with their current values. A circle object, for example, has a data field
radius, which is the property that characterizes a circle. A rectangle object has data
fields width and height, which are the properties that characterize a rectangle.
■ The behavior of an object (also known as its  actions) is defined by methods. To
invoke a method on an object is to ask the object to perform an action. For example,
you may define a method named getArea() for circle objects. A circle object may
invoke getArea() to return its area.
Objects of the same type are defined using a common class. A class is a template, blueprint,
or contract that defines what an object’s data fields and methods will be. An object is an
instance of a class. You can create many instances of a class. Creating an instance is referred
to as instantiation.

一个对象代表了现实世界中的一个独特的实体,例如一个学生,一个按钮,一个圆。每个对象拥有自己独特的 属性与行为,例如 一个学生有他的年龄,身高等,而这个学生可以有自己的睡眠习惯,衣着习惯等。同一类型的对象抽象起来就是一个类,类就是蓝图,模板。一个对象是类的一个实例,你可以用同一个类创建许多不同的对象,例如你可以用一个圆的类实例化不同直径大小的圆。

java使用变量来定义属性,用方法来定义动作,此外,类还提供一种叫做构造方法的特殊方法,调用它就可以创建对象。它的初衷是用来初始化对象。

例如:

class Circle
{
	double r=1.0;
	//构造方法1.这样创建出来的圆就是默认的r=1.0
	Circle()
	{
		
	}
	//构造方法2,自己传入特定的半径值
	Circle(double r)
	{
		this.r=r;
	}
	double getArea(double r)
	{
		return r*r*Math.PI;
	}
}

2 构造方法
Constructors are a special kind of method. They have three peculiarities:
■ A constructor must have the same name as the class itself.
■ Constructors do not have a return type—not even void.
■ Constructors are invoked using the new operator when an object is created. Con-
structors play the role of initializing objects.
The constructor has exactly the same name as the defining class. Like regular methods, con-
structors can be overloaded (i.e., multiple constructors can have the same name but different
signatures), making it easy to construct objects with different initial data values.

构造方法是一种特殊的方法,他有特殊的格式。要跟定义的类同名;一定要切记构造方法没有返回值,也不会有void这样的修饰符; 构造方法可以overload,即一个类中我们可以定义参数列表不同的构造函数。当我们在创建一个对象时候,我们需要用new 然后调用构造方法;

一定一定切记构造方法没有返回值,很多时候我们最常见的错误u就是给他加上一个void,这是错误的,加上void后他就不再是构造方法,而是这个类的一个普通方法了。

例如:

public  void Circle() {
}

这样多了个void 他就不再是构造方法,而是一个普通方法了。

A class normally provides a constructor without arguments (e.g., Circle()). Such a con-
structor is referred to as a no-arg or no-argument constructor.
A class may be defined without constructors. In this case, a no-arg constructor with an
empty body is implicitly defined in the class. This constructor, called a default constructor, is
provided automatically only if no constructors are explicitly defined in the class.

一般情况下,一个类会提供一个public ClassName(){ } 这样的没有参数的构造函数。但有时候一个类可能连一个构造函数都见不到,这个时候,类中隐含了没有参数的构造函数且函数体为空的构造函数,这个构造函数叫默认构造函数。该默认的构造函数在类中没有明确给出构造函数时候会自动提供。也就是说,如果类中已经有定义的一些构造函数,例如可能给的构造函数都是有参数的,这时候这个默认的无参数的构造函数是不会存在的,也就是说这时候是不能适用这种方式来创建对象的。

Case Study:An TV class

Each TV is an object with states (current channel,
current volume level, power on or off) and behaviors (change channels, adjust volume, turn
on/off). You can use a class to model TV sets.

其UML如下:


public class TV
{
	int channel=1;
	int volume=1;
	boolean on=false;
	public PV()
	{
		
	}
	public void turnOn()
	{
		this.on=true;
	}
	public void turnOff()
	{
		this.off=false;
	}
	public void setChannel(int newChannel)
	{
		this.channel=newChannel;
	}
	public void setVolume(int volume)
	{
		this.volume=volume;
	}
	public void channelUp()
	{
		this.channel++;
	}
	public void channelDown()
	{
		this.channel--;
	}
	public void volumeUp()
	{
		this.volume++;
	}
	public void volumeDown()
	{
		this.volume--;
	}
}
3 通过引用变量访问对象

我们可以通过new来创建一个新的对象,新创建的对象会被分配内存。类也是一种引用数据类型,我们通过引用变量来访问对象。例如我们可以使用 TV myTV=new TV();这样新建一个对象,这时候会在内存(堆上)分配该对象所需要的内存空间,而会返回一个引用变量,赋值给myTV,那么myTV就指向了这个对象所在的内存空间块。

4 类中属性默认的初始值

The default value of a data field is null for a reference type, 0 for a numeric type, false
for a boolean type, and '\u0000' for a char type. However, Java assigns no default value
to a local variable inside a method. 

我们在类中的属性,可以是基本数据类型例如int,char,Boolean等,也可以是String,array等这样的引用类型。当我们没有人为进行初始化时候, 对于数字型类型的我们初始化为0,Boolean类型的初始化为false,char类型会初始化微'\u0000',而如果是一个引用类型的属性并没有指向任何对象时候,会被初始化为null;

class Student
{
	String name; //默认初始化为null
	int age;//默认初始化为0;
	boolean ifUniversity; //默认初始化为false
	char gender;//默认初始化为‘\u0000’
}

5 基本类型与是引用类型的区别

数据类型有基本类型或者是引用类型之分,那么他们有什么区别呢?

 For a variable of a reference type, the value is a reference to where an object is located

所以,对于一个引用类型变量,他的值是指向该对象所在的位置

这样如果我们使用 Circle newC=C时候,其实就是新建了一个NewC的引用变量的空间,而该引用变量同样指向了我们上面的 Circle C =new Circle() 对象的空间。

最后tip:

当你不再需要某个对象时候,你可以将该对象引用变量赋值null,这样JVM就会自动回收这些没有被引用变量指向的对象空间了

If you know that an object is no longer needed, you can explicitly assign null to a reference vari-
able for the object. The JVM will automatically collect the space if the object is not referenced by
any reference variable.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值