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.

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值