java.对象和类(1)

一、类和对象的基本概念
类: 类似于c语言中的结构体。是一种成员,函数等的集合体,类中定义的是对象的状态和行为。
对象: 表示为特性的表示,每个个体依据不同的特性有不同的对象
对象的状态: 使用数据描述性质,例如用radius描述半径。
对象的行为: 要求对象做某些事,例如用getArea()来获取面积,用setRadius(radius)修改半径。

二、类的定义和使用

其他副类应建立于public主类之外

代码中只能有一个主类(public),编译后,文件名为public类名,其他类编译成class文件

主函数中使用操作符.来对类中的元素引用
在java中定义类,使用关键字class

class 类名称 {
         属性 (变量) ;
         行为 (方法) ;
}
//定义一个Circle类
class Circle
{
    double radius;
    double getArea()//获取面积
    {
        return Math.PI * radius * radius;
    }
    double setRadius(newRadius)//重新赋值半径
    {
        radius = newRadius;
    }

}

定义好类之后,使用类可以通过以下两种格式来声明对象

①类名称 对象名称 = new 类名称 () ;
Circle circle1 = new Circle();//声明并实例化对象

②类名称 对象名称 = null ;
对象名称 = new 类名称 () ;//先声明再实例化对象
Circle circle1 = null;
circle1 = new Circle();

让我们来看一个例子

package define_lei_duixiang;

public class lei_duixiang {

	public static void main(String[] args) {
		Circle circle1 = new Circle();
		System.out.print("The area of the circle of radius" + circle1.radius + "is" + circle1.getArea()+ "\n");
		Circle circle2 = new Circle(25);
		System.out.print("The area of the circle of radius" + circle2.radius + "is" + circle2.getArea()+ "\n");
		Circle circle3 = new Circle(100);
		System.out.print("The area of the circle of radius" + circle3.radius + "is" + circle3.getArea()+ "\n");
		circle1.radius = 100;
		System.out.print("The area of the circle of radius" + circle2.radius + "is" + circle2.getArea()+ "\n");
	}
}
	
	class Circle
	{
		double radius ;//实例变量
			Circle()
			{
				radius = 1;
			}
			
			
		    Circle(double Radius)
			{
				radius = Radius;
			}
			
			
			double getArea()//实例方法
			{
				return (Math.PI * radius *radius);
			}
			
			
			double getPerimeter()
			{
				return 2 * Math.PI * radius;
			}
			
			
			void setRadius(double Radius)
			{
				radius = Radius;
			}
			
	}

运行结果

The area of the circle of radius1.0is3.141592653589793
The area of the circle of radius25.0is1963.4954084936207
The area of the circle of radius100.0is31415.926535897932
The area of the circle of radius25.0is1963.4954084936207
class Person
{
	String name;
	int age;
	public void tell()
	{
	    System.out.printf("姓名:" + name +" 年龄:" + age)}
}
public class Testclass
{
    public static void main(String args[])
    {
    	Person per = new Person();//声明并实例化对象
    	//Person per = null;  先声明对象
    	//per = new Person();  再实例化
    	per.name = "狗蛋";
    	per.age = 11;
    	per.tell();
    }
}
姓名:狗蛋 年龄:11

三、对象的引用传递

class Person
{
	String name;
	int age;
	public void tell()
	{
	    System.out.printf("姓名:" + name +" 年龄:" + age)}
}
public class Testclass
{
    public static void main(String args[])
    {
    	Person per1 = new Person();//声明并实例化对象
    	Person per2 =per1;
    	per1.name = "张三";
    	per1.age = 11;
    	per2.name = "李四";
    	per1.tell();
    }
}
姓名:李四 年龄:11

为什么给per2.name赋值会修改per1.name呢?
这就要牵扯到两个概念
堆内存:保存属性的内容,需要通过new方法来分配内存
栈内存:保存堆内存的地址,也就是对象的名字
对应内存配图如下
在这里插入图片描述
刚开始,per1与per2指向同一堆内存,所以修改per2就修改了per1

我们再来看看另一个例子

class Person
{
	String name;
	int age;
	public void tell()
	{
	    System.out.printf("姓名:" + name +" 年龄:" + age)}
}
public class Testclass
{
    public static void main(String args[])
    {
    	Person per1 = new Person();//声明并实例化对象
    	Person per2 = new Person();
    	per1.name = "张三";
    	per1.age = 11;
    	per2.name = "李四";
    	per2.age = 22;
    	per2 = per1;
    	per2.name = "王五"
    	per1.tell();
    }
}

对应内存配图如下
在这里插入图片描述
因为主类中使用了两个new,所以就分配了两个堆内存。per1和per2指向俩个不同的堆内存。
后将per2 = per1。二者指向同一堆内存。

此时没用的堆内存也就变成了垃圾

知识来源链接

四、构造方法构造对象
构造方法是用来构造对象的

public void Circle()
{
}

这叫做方法,而非构造方法。

构造方法原则:
①构造方法必须和所在类名字相同
②构造方法没有返回值,甚至连void都没有
③构造方法是在创建对象时使用的,用操作符new调用,目的是初始化对象

例如上文中的

Circle circle1 = new Circle();

就是构造方法创建对象。

无参构造方法通常,类中会提供一个没有参数的构造方法,称为无参构造方法(也就是说,此方法需要自己在类中定义)
默认构造方法当一个类中没有构造方法时,系统会自动提供一个构造方法。

public class Dog1
{}

public class Dog2
{
	public Dog2(String s)
	{}
}

public class Dog3
{
	public Dog3()
	{}
}

Dog1 dog1 = new Dog1();
Dog2 dog2 = new Dog2();
Dog3 dog3 = new Dog3();
正确
错误
正确
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值