Cov txheej txheem:

Txoj kev twg koj yuav tsum override yog tias koj siv lub runnable interface?
Txoj kev twg koj yuav tsum override yog tias koj siv lub runnable interface?

Video: Txoj kev twg koj yuav tsum override yog tias koj siv lub runnable interface?

Video: Txoj kev twg koj yuav tsum override yog tias koj siv lub runnable interface?
Video: Leng Yang - Yog Vim Dab Tsi (Official Full Song | Nkauj Tawm Tshiab) 2020/05/24 2024, Kaum ib hlis
Anonim

Ib chav kawm ntawd Implements Runnable tau khiav tsis muaj subclassing Xov los ntawm instantiating ib Thread piv txwv thiab dhau nws tus kheej nyob rau hauv raws li lub hom phiaj. Feem ntau, cov Runnable interface yuav tsum siv yog koj yog tsuas yog npaj rau override khiav () txoj kev thiab tsis muaj lwm txoj xov txoj kev.

Tom qab ntawd, leej twg ntawm cov txheej txheem no yog siv los ua kom siv tau qhov kev sib tshuam?

Piav: To siv Runnable interface , chav kawm tsuas yog xav tau siv ib ib txoj kev hu ua run().

Tsis tas li ntawd, koj yuav siv Runnables li cas? Los ntawm siv Runnable interface, koj yuav tsum muab kev siv rau kev khiav () txoj kev. Kom khiav qhov no kev siv class, tsim ib tug xov object, pass Kev siv tau yooj yim class object rau nws constructor. Hu rau start() method on thread class to start executing run() method.

Ib sab saum toj no, dab tsi yog cov txheej txheem hauv runnable interface?

Khiav tau yog ib interface uas yog yuav tsum tau muab coj los siv los ntawm ib chav kawm uas nws qhov xwm txheej yuav tsum tau ua los ntawm ib txoj xov. Muaj ob txoj hauv kev los pib Xov Xwm tshiab - Subclass Xov thiab siv Khiav tau . Tsis tas yuav tsum tau subclassing Xov thaum ib txoj hauj lwm yuav ua tau los ntawm overriding nkaus xwb khiav() txoj kev ntawm Khiav tau.

Yuav ua li cas kuv pib lub runnable interface xov?

Txhawm rau siv Runnable interface los tsim thiab pib xov, koj yuav tsum ua cov hauv qab no:

  1. Tsim ib chav kawm uas siv Runnable.
  2. Muab ib txoj kev khiav hauv chav Runnable.
  3. Tsim ib qho piv txwv ntawm Thread class thiab dhau koj cov khoom Runnable rau nws tus neeg tsim khoom raws li qhov ntsuas.
  4. Hu rau Thread object's start method.

Pom zoo: