Java OOP
OOP kirefu chake ni Object Oriented Programming
Procedural Programming inahusu kuandika procedures au method ambazo zina fanya operation na data, ambapo object oriented programming ni inahusu kutengeneza objects ambayo inabeba data na methods.
Kwenye Java, What is classes and objects?
Classes na Objects ni vitu viwili muhimu sana kwenye object oriented programming , unaweza kutazama onyesho hapo chini
Class ---- tunda
Objects ---- epo, ndizi , chungwa
onyeshi lingine
Class ------ gari
Objects ---- Volvo, Toyota, suzuki
Kwahiyo class ni muundo kwa ajili ya objects na objects ni inaundwa kwa mfano wa class ( muundo)
Ikitengenezwa object moja basi hutumia variable na method zote za class
Java class and objects
na hapa ikumbukwe kwamba java ni object oriented programming language : Kila kitu kwenye java kinahusiana na objects na classes zikiwemo na attributes na methods kwa mfano kwenye maisha ya kawaida , gari ni object. gari lina attributes zake kama vile uzito na rangi , na vilevile lina methods zake kama vile kuendesha na breki. Class ni kama object constructor yaani ni kama muundo wa kutengeneza vitu ( objects)
Create a class
ili kutengeneza class tunatumia keyword ya class
Main.java
tengeneza class yenye jina Main na iwe na variable x.
public class Main {
int x = 5 ;
}
kumbuka katika java syntax jina la class lazima herufi ya kwanza iwe kubwa na jina la faili ya java lazima lifanane na jina ya class yako
Tengeneza objects
kwenye java object hutengenezwa kutoka kwenye class . Ambapo tayari tumesha tengeneza class yenye jina Main na sasa tunaweza kutumia kutengeneza object moja au zaidi.
mfano
Tengeneza object inayoitwa myObj na print value ya x
public class Main {
int x = 5 ;
public static void main ( String [] args ) {
Main myObj = new Main () ;
System.out.println(myObj.x);
}
}
output: 5
Multiple Objects
unaweza kutengeneza object nyingi kwenye class moja
mfano : tengeneza object mbili za class Main
public class main {
int x = 5;
public static void main (String []args){
Main myObj1 = new Main();
Main myObj2 = new Main();
System.out.println( myObj1.x);
System.out.println( myObj2,x);
}
}
Output: 5
// 5
Kutumia multiple classes
vilevile unaweza kutengeneza object ya class na ukaitumia ndani ya class ingine tofauti na hii ya kawaida inatumika ili kurahisisha na kuweka class kwenye mpangilio bora ambapo class moja inakuwa na attributes na methods tu na ingine inashikilia main() method ( yaani code ambazo zinatakiwa kuwa executed)
kumbuka jina la faili yako ya java lazima lifanane na jina la class ya java , kwenye mfano huu tumetengeneza faili mbili kwenye folder moja\\
Main.java
Second. java
Main.java
Second.java
Main.java
public class main {
int x = 5;
}
Second.java
class Second {
public static void main ( String [] args){
Main myObj = new Main ();
System.out.println(myObj.x);
}
}
output: 5
JISAJILI HAPA KUPATA MUONGOZO WA TAALUMA ZA JUU ZA TEHAMA
Java class Attributes
Kwenye somo lililopita tulikuwa tuna tumia neno variable kuita x kwenye mfano lakini ndio hiyo hiyo attribute ya class( attribute kwa kiswahili ni kama sifa ya kitu)
Au unaweza sema attributes za class ni variable ndani ya class.
mfano tengeneza class iite Main ambayo iwe na attribute x na y
public class Main {
int x = 5;
int = 3;
}
jina lingine la attributes za class huitwa fields
Accessing Attibutes
ili uweze ku access attributes unatakiwa kutengeneza objects ya class na ukitumia kanuni ( dot syntax (.) ) Mfano ufuatao tutatengeneza object ya Main class, object tutaiita myObj na tutaprint value ya x attribute
mfano
tengeneza object iite myObj na i print value ya x
public class Main {
int x =5;
public static void main ( String [] args) {
Main myObj = new Main ();
System.out.println(myObj.x);
}
}
Mwisho wa somo la 14
0 Comments