类和对象_3
1.什么是对象?
1.对象是由我们自己创建的类来创建出来的。[没有类就没有对象]
2.对象就是类的实例【可以真正使用类的数据,可以调用类中元素的数据】
例如:修建大楼是绘制的设计图纸就是类,根据设计图纸修建起来的真实的可以住人的大楼就是对象。
3.一个类可以创建出多个对象
类是对象的模板,对象是类的真实表现
2.如何创建对象?
1.保证用来创建对象的类是存在
2.需要通过类中提供的构造方法创建对象
格式:new 构造方法( [参数值] );
public class Person{
public static void main(String args[]){
//创建Person类的对象
//格式: new 构造方法([参数值])
//new Person();//Person类的对象
//new Person("javase");
//为了以后方便使用对象,我们就把这个对象保存在变量
Person per1=new Person();
}
public Person(){
System.out.println("Person类无参数构造方法");
}
public Person(String value){
System.out.println("Person类有参数构造方法,参数是--value=="+value);
}
}
3.创建好的对象的有何作用?
创建好的对象可以访问/调用类中的元素【变量/方法】,至于创建好的对象可以访问/调用类中的那些元素,那些元素不能访问/调用,就得看类中元素的访问限制修饰符。
4.对象的具体用法
变量的访问
1.实例变量—【对象.实例变量】
2.静态成员变量–【对象.静态成员变量/类名.静态成员变量】
对变量只存在得到变量的值使用或者是修改变量值这两种的操作。
以上的访问是在public修饰符的作用下操作的。
例如:
class Student{
//实例变量
public int stuid=1001;
public int stuage;
//静态成员变量
public static String stuname="zhangsan";
public static String stuaddress;
}
public class Test2{
public static void main(String args[]){
//访问实例变量---【对象.实例变量】
//创建Student类的对象
Student stu1=new Student();
System.out.println("stuid=="+stu1.stuid);
//赋值
stu1.stuage=23;
System.out.println("stuage=="+stu1.stuage);
//访问类变量--【对象.类变量/类名.类变量】
Student stu2=new Student();
System.out.println("stuname=="+stu2.stuname);
System.out.println("stuname=="+Student.stuname);
//stu2.stuaddress="西安";
//System.out.println("stuaddress=="+stu2.stuaddress);
//System.out.println("stuaddress=="+Student.stuaddress);
Student.stuaddress="北京";
System.out.println("stuaddress=="+stu2.stuaddress);
System.out.println("stuaddress=="+Student.stuaddress);
}
}
方法的访问
1.构造方法—new 构造方法();
2.实例方法–【对象.实例方法】
3.静态方法–【对象.静态方法/类名.静态方法】
以下使用无返回值,无参数的方法为例
例如:
class Dog{
//实例方法
public void dogTest1(){
System.out.println("Dog类中的实例方法");
}
//类方法
public static void dogStatic(){
System.out.println("Dog类中的类方法");
}
}
public class Test3{
public static void main(String args[]){
//实例方法--【对象.实例方法】
Dog dog1=new Dog();
dog1.dogTest1();
//静态方法--【对象.静态方法/类名.静态方法】
Dog dog2=new Dog();
dog2.dogStatic();
Dog.dogStatic();
}
}
有参数的方法访问,需要注意的是参数的传递
例如:
class Cat{
//实例方法
public void catTest1(int age,double height){
System.out.println("Cat类中的实例方法--参数1--age=="+age+",参数2--height=="+height);
}
//类方法
public static void catStatic(Dog dog){
System.out.println("Cat类中的类方法--参数--dog=="+dog);
}
}
class Dog{
}
public class Test4{
public static void main(String args[]){
//实例方法--【对象.实例方法】
Cat cat1=new Cat();
cat1.catTest1(5,12.5);
//类方法
Cat.catStatic(new Dog());
//上面的方法在调用的时候传递的参数是具体的数据值
//我们在访问访问的时候可以传递保存数据值的变量
int age=3;
double hei=6.7;
cat1.catTest1(age,hei);
Dog dog=new Dog();
cat1.catStatic(dog);
}
}
总结: 先看【有没有/有几个/什么类型】,再传【匹配类型/匹配个数/值【变量】】
有返回值的方法访问,需要注意的是接收方法的返回值结果【类型匹配】
例如:
class Cat{
//实例方法
public int catTest2(int num1,double num2){
int res=0;
res=(int)(num1+num2);
return res;
}
//类方法
public static Student catStatic2(Dog dog){
//return new Student();
Student stu=new Student();
return stu;
}
}
class Dog{
}
class Student{
//实例变量
public int stuid=1001;
}
public class Test4{
public static void main(String args[]){
//访问有返回值和参数的方法
Cat cat2=new Cat();
int val=cat2.catTest2(12,12.5);
System.out.println("catTest2的运行结果=="+val);
Student stu=Cat.catStatic2(new Dog());
System.out.println("catStatic2的运行结果=="+stu.stuid);
}
}
总结:方法的调用【1.要什么,传什么;2.给什么,收什么】
5.对象的内存分配原理
对象是通过类的构造方法创建
类是一种自定义的复合数据类型
6.类与对象的关系
类是创建对象的模板;对象是类的具体表现