首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android札记——Day2 *Java中类的继承 *多线程Handler

2012-09-25 
Android笔记——Day2 *Java中类的继承 *多线程Handler1、Java中类的继承。··Java中类的继承所产生的访问权限的

Android笔记——Day2 *Java中类的继承 *多线程Handler

1、Java中类的继承。

··Java中类的继承所产生的访问权限的问题和C++中不太一样。

··Java中多了一个包的概念。类中成员的访问权限多了一个default的访问权限。(默认不写的访问权限)

··在Java同一个包中,只要成员不是以private修饰的,包中类与类之间都可以相互访问。

··一个类只有声明为public才可以被其他包中的类所发现,才可以进行导入。声明为public时,文件名必须和类名一样。因此一个文件中只能有一个public类。

··protected具有所有default所具有的访问权限。protected在不同包中继承之后可以被访问,而default不行(即使已经导入了)

··Java中使用interface实现多重继承。interface中都是public的抽象函数。实现接口用关键字implements。

··个人认为好的编程习惯有:

1)一个文件中尽量只有一个类,并且为public类。

2)不加声明的成员尽量赋以protected权限,方便日后的继承。

3)等日后具体编程的时候再总结吧...

测试代码如下...

?

//hello.java和test2.java在同一个包中,test.java在另一个包内public class hello {    int a = 10;    private void print(){System.out.println(a);}}
import soga.test;public class test2 {public static void main(String srgs[]){hello aa = new hello();aa.print();//此句报错}}
public class test extends SEU.hello{void hhhh(){hello aaaa = new hello();this.print();}protected void print(){System.out.println("adsa");}public void seta(int a){this.a = a;//报错}public void out(){System.out.println(a);//报错}}

?

?

2、多线程

··Java中的多线程的实现可以有两种方法,一种是继承Thread类,还有一种是实现Runnable接口。(实际上Thread就是继承了Runnable接口的。

··具体过程:

1)新写一个类实现Runnable接口,并重写其中的run()函数,run()函数中就是要在另外一个线程中完成的内容。

2)创建出一个上面新写的类的对象,使用Thread(该对象)生成一个新的线程对象,调用该线程对象的start()方法便可运行该线程。

3)想要让某个线程暂时停下一会可以使用Thread.sleep(长整形数) 的方法。

4)继承Thread类实现多线程的方法类似。无需有Thread(该对象)这一步。

代码如下:

?

public class thr {public static void main(String args[]){newthread thread1 = new newthread();new Thread(thread1).start();//生成新的线程对象并调用start()方法try{Thread.sleep(1);}catch (Exception e) {}System.out.print("hello android");}}class newthread implements Runnable{public void run(){for(int i=0;i<=10;i++){System.out.print(i);}}}//运行的结果为“01234567hello android8910”
?

Android中的多线程:

Android中的多线程常常借助于Handler实现,由于Android多线程中可能会对布局界面进行改变,而Android貌似不允许在Thread或Runnable中对布局进行改变。实现的一般方法是在Runnable的run()函数中向一个Handler对象(取名为handler)发送消息进入队列,handler中handleMessage(Message msg)方法会对消息进行处理,在这里对布局进行改变。是某个多线程反复不断地做,可以再改Runnable(取名为run1)对象的run()方法中加上“handler.postDelayed(run1,几毫秒后加进handler中)”。

我写了个类似秒表的程序(不是精确的),用多线程实现,实现开始,暂停,和结束功能。代码如下:

?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"/>    <EditText        android:text="0"        android:id="@+id/num"        android:layout_width="match_parent"        android:layout_height="wrap_content"       />    <LinearLayout        android:id="@+id/linearLayout1"        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <Button            android:id="@+id/start"            android:layout_width="fill_parent"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="START" />        <Button            android:id="@+id/stop"            android:layout_width="fill_parent"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="STOP" />        <Button            android:id="@+id/end"            android:layout_width="fill_parent"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="END" />    </LinearLayout></LinearLayout>

package com.seu;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class CountppActivity extends Activity {EditText num;long count = 0;Handler handler = new Handler(){public void handleMessage(Message msg) {//消息处理;switch (msg.what){case 0:num.setText(String.valueOf(count));break;default:super.handleMessage(msg);break;}}};    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button startButton,stopButton,endButton;        startButton = (Button)findViewById(R.id.start);        stopButton = (Button)findViewById(R.id.stop);        endButton = (Button)findViewById(R.id.end);        num = (EditText)findViewById(R.id.num);        startButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {new Thread(run1).start();//开始执行run1;}});        stopButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {handler.removeCallbacks(run1);handler.removeMessages(0);//清除handler队列中0的消息;}});        endButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {handler.removeCallbacks(run1);//清除handler队列中某线程;count = 0;handler.sendEmptyMessage(0);}});    }    Runnable run1 = new Runnable() {@Overridepublic void run() {count++;handler.sendEmptyMessage(0);handler.postDelayed(run1, 1);//反复循环叠加;}};}
?

?

?

?

热点排行