| 
 
| [ 本帖最后由 Himoriarty 于 2015-6-10 14:29 编辑 ]\n\n[ 本帖最后由 Himoriarty 于 2015-6-10 14:26 编辑 ]\n\n<a  target="blank">java培训</a>、期待与您交流! ---------- 面向对象:1、什么时候使用静态?因为静态修饰的是成员变量和函数,当对象中出现共享数据时,该数据被静态修饰,对象中的特有数据要定义成非静态存在于堆内存中。2、什么时候定义静态函数?当功能内部没有访问到非静态数据(对象的特有数据),那么该功能可以定义为静态。
 3、建立对象时,如:Person p = new Person();这句话都做了什么事情:                                              1)、因为new用到了Person,class,所以会先找到Person。class文件并加载到内存中。                                               2)、执行该类中的是static 代码块如果有的话,给Person。class类进行初始化。                                               3)、在堆内存中开辟空间,分配内存地址。                                             4)、在堆内存中建立对象的特有属性,并进行默认初始化。                                                          5)、对属性进行显示初始化;                                                   6)、对对象进行构造代码块初始化;                                                 7)、对对象进行对应的构造函数初始化                                                  8)、将内存地址给栈内存中的P变量。
 [code]/*private 私有只在本类中有用
 将age私有化以后,类以外及时建立了对象也不能直接访问,
 但是人应该有年龄。就需要在person类中提供对应访问age的方式
 
 注意:私有仅仅是封装的一种表现形式
 
 对象一建立就会调用与之对应的构造函数
 构造函数的作用:可以用于给对象进行初始化
 
 构造函数的小细节:
 当一个类中没有定义构造函数,那么系统会默认给该类加入一个空参数的构造函数
 */
 class Person
 {
 private String name;
 private int age;
 
 Person()
 {
 System.out.println("name="+name+",age="+age);
 }
 }
 
 
 
 
 class PersonDemo
 {
 
 public static void main(String[] args)
 {
 Person p = new Person();
 
 }
 
 }[/code]
 练习心得:本人觉得采用if语句调试可以避免出现注释引起的问题,且可以嵌套调试不影响代码,
 相比采用注释的方法调试程序更严谨,方便。
 [code]class TV
 {
 static int channel = 1;  /*初始化频道*/
 static int volume = 1;  /* 初始化音量*/
 String volumelevel = "Novoice";
 static Boolean on = false;
 
 public void TV()
 {
 
 }
 
 public void turnOn()
 {
 on = true;
 }
 
 public void turnOf()
 {
 on = false;
 }
 //设置频道
 public static void setChannel(int newChannel)
 {
 if(on && newChannel > 1 && newChannel < 120)
 {
 channel = newChannel;
 }
 }
 //设置音量
 public void setVolume(int newVolume)
 {
 if(on && newVolume > 1 && newVolume < 7)
 {
 volume = newVolume;
 }
 
 }
 //加频道
 public static void channelUp()
 {
 if(on && channel < 120)
 {
 channel++;
 }
 }
 //减频道
 public void channelDown()
 {
 if(on && channel > 1)
 {
 channel--;
 }
 }
 //增加音量
 public void volumeUp()
 {
 if(on && volume < 7)
 {
 volume++;
 }
 }
 //降低音量
 public void volumeDown()
 {
 if(on && volume >= 1)
 {
 volume--;
 }
 if(on && volume <= 0)
 {
 System.out.println("Novoice");
 }
 }
 }
 
 
 public class TestTV
 {
 public static void main(String[] args)
 {
 TV tv1 = new TV();
 
 tv1.turnOn();
 tv1.setChannel(29);
 tv1.setVolume(4);
 System.out.println("tv1's channnel is " + tv1.channel
 + " and volume level1 is " + tv1.volume);
 
 //调试1
 if(true)
 {
 TV tv2 = new TV();
 
 tv2.turnOn();
 //嵌套调试1
 if(false)
 {
 tv2.channelUp();
 tv2.channelUp();
 tv2.volumeDown();
 }//嵌套调试1结束
 
 }//调试1结束
 
 
 //打印对应频道和音量
 System.out.println("tv1's channnel is " + tv1.channel
 + " and volume level1 is " + tv1.volume);
 
 //调试2开始
 if(false)
 {
 System.out.println("tv2's channnel is " + tv2.channel
 + " and volume level1 is " + tv2.volume);
 }
 //调试2结束
 
 }
 }[/code]
 
 ----------android培训、java培训、java学习型技术博客、期待与您交流!------------
 | 
 |