Class and object

I am sorry to delay writing the summary.

 

Q1:what is object?

A1:Object must be an concret.For example,girl can refer to a wide range,but this girl named xuying is an concret.Also,we often describe an object in two aspects:characteristic and action.For example,the girl's name,age is her characteristics and studying is her action.

  We all know the world comprises numerous objects,and these objects can be divided into many classes according to their similarities.

 

Q2:What is class?

A2:Class is a template.Its contents are based on the real objects.Class contains objects' attributes and fuctions.Attributes refer to characteristics and fuctions refer to actions.We can add the objects' attributes and fuctions into the class,so when we use class to new an object,it will be convenient to call the object's functions and assign its attributes.

 

Q3:Can object act as a parameter to be delivered?

A3:Of course it can.The class we define is also a data type,so the class can be the type of parameter and the object can be a parameter.

 

Here are some codes.

//This is a  class of Character.
 //name of parameters
 private String name;
 private int blood;

 

Q4:Why use private to set all member variables' access right in the class,not public?

A4:For the sake of safety.Private makes member variables can be used only in its own class,other classes can't use them,even in its extend class.For example,you have a name attribute,if public,everyone can change your name,if private,only you can change it,which one do you prefer?

 

//设置角色的名字属性
 public void setName(String n){
  name = n;
 }
 //获得角色的名字属性
 public String getName(){
  return name;
 }

 

The funcions above are different,one has parameter and the other doesn't.



 

 

 

 

 


Next lesson we study the class and object further.

 

Q5:What's the difference between common method and construction method?

A5:1、the name of construction method must be the same with the class,it doesn't have  data type of  the value returned.2、construction method is used to instantiate an object and assign its attributes while common method is the action of object.

Tips:

Every system has a default construcion method,when we define a new construction method,the default is covered automatically.

 

Q6:What is method overloading?

A6:First is its conditions:1、method's name must be the same.2、parameter has at least one diffenfent aspect: number、type、sequence.Next is whywe need method overloading?For a easy example,some PC has camera inside while some doesn't.Their parameter is different  in the number.

 

Q7:What's the difference between passing by value and reference?

A7:Basic data tpye follows the rules of passing by value.Let's talk about passing by reference.Student st1  =new Student(); When we new Student(),it return the address,the address is assigned to the variable st1.So st1 points to the new object.

 

 

 

 

 
展开阅读全文

没有更多推荐了,返回首页