紹興網(wǎng)站建設(shè)多少錢僅需500元(代碼大全的內(nèi)容)代碼意思大全,
最終為多少?11111package beforetheclass;public class static05 { public static void main(String[] args) {
//調(diào)用了雙參構(gòu)造方法,并遇見new完成了類加載; Cat1 c = new Cat1(28, "紫色"); //最終完成輸出age和color System.out.println(c.age);
System.out.println(c.color); }}class Cat1 { //最大的作用就是兩個輸出語句自上而下依次進(jìn)行還有就是各種干擾作用 { age = 18;
System.out.println("age building block"); } int age = 10; String color = "黃色"; { color = "黑色";
System.out.println("color building block"); } //進(jìn)2,age為28,1.最先輸出本參語句 public Cat1(int age) {
System.out.println("age constructor"); this.age = age; } //進(jìn)1,遇見單參構(gòu)造方法,age為28,2.回到雙參,輸出了本參語句
public Cat1(int age, String color) { this(age); System.out.println("age color constructor");
this.color = color; }}//答案是紫色,282.count為多少?11111public class Demo2 { public static void main(String[] args) {
//循環(huán)了3次,創(chuàng)建了3個對象,count自增三次 //但是進(jìn)入構(gòu)造方法后,遇到count會回到類首聲明處尋找賦值,于是回到類首處賦值count又自增了 //如此往復(fù),一次循環(huán)自增兩次最終結(jié)果count為6
for (int i = 0; i < 3; i++) { //new觸發(fā)了類加載,因此count賦值為0 new Dog(); } //觸發(fā)單參構(gòu)造器,一共循環(huán)兩次,而每次自增3,最終結(jié)果為12
for (int i = 0; i < 2; i++) { new Dog(10); } //輸出全局變量count System.out.println(Dog.count);
}}class Dog{ int num = 10;//進(jìn)2前會先進(jìn)行顯式賦值,因此會獲取數(shù)值 static int count = 0; { // 復(fù)雜的計(jì)算給num賦值時,可以用構(gòu)造代碼塊
// "提取公因式" count++; }//進(jìn)2,碰見count后,會到外部類的開始賦值處進(jìn)行 public Dog() { count++;
}//進(jìn)1,調(diào)用單參方法,計(jì)數(shù);碰到this()后進(jìn)入無參構(gòu)造器 public Dog(int num) { this(); this.num = num; count++;
}}count為123.創(chuàng)建一個類成員變量:String name,int stuId,String gender,int age,double score(表示學(xué)生Java考試成績)構(gòu)造器:無參構(gòu)造器,全參構(gòu)造器等,自由發(fā)揮。
成員方法:print(),打印對象所有屬性的取值類定義完畢后,創(chuàng)建五個Student對象存入Student對象數(shù)組中,完成以下兩個操作:遍歷輸出每個學(xué)生的屬性信息(遍歷對象調(diào)用print()方法)求成績平均值。
注:數(shù)組既可以存儲基本數(shù)據(jù)類型(的值),也可以存儲引用數(shù)據(jù)類型(的引用)它們的使用沒有本質(zhì)區(qū)別可以考慮提取方法實(shí)現(xiàn)功能,不要把所有代碼一股腦寫在main方法中1111package beforetheclass;。
public class homework01 { public static void main(String[] args) { //以下為用對象給數(shù)組賦值******* Student[] studs = new Student[5];
//創(chuàng)建5個對象,用調(diào)用構(gòu)造器的方法給數(shù)組賦值 Student s1 = new Student(1,"","",1,1); Student s2 = new Student(1,"","",1,1);
Student s3 = new Student(1,"","",1,1); Student s4 = new Student(1,"","",1,1); Student s5 = new Student(1,"","",1,1);
studs[0] = s1; studs[1] = s1; studs[2] = s1; studs[3] = s1; studs[4] = s1;
print0(studs); } public static void print0(Student[] studs){ for (Student student : studs) {
student.print(); } } public static double getAverage(Student[] arr){ double sum = 0;
for (Student student : arr) { sum += student.score; } return (sum / arr.length);
}}class Student{ int id; String name; String gender; int age; double score; public Student(){
} public Student(int id, String name,String gender, int age){ this.id = id; this.name = name;
this.gender = gender; this.age = age; } public Student(int id,String name, String gender,int age,double score){
this(id, name, gender, age); this.score = score; } public void print(){ System.out.println("名字:" + this.name + "學(xué)號:" + id + "性別:" + gender + "年兩:" + age +"分?jǐn)?shù):" +score);
}}4. 封裝的練習(xí)1package aftertheclass;public class _71301 { public static void main(String[] args) {
Grade grade = new Grade(); //由于沒有賦值,因此輸出的是默認(rèn)值0 System.out.println(grade.getAge());
//封裝后調(diào)用賦值 grade.setAge(12); //最終輸出值12,凡人 System.out.println(grade.getAge());
} }class Grade{ private int age; public int getAge(){ return age; } //看看你的需求,是向外輸出相應(yīng)的結(jié)果!因此只要用setage即可;
public void setAge(int age){ if(age > 112){ System.out.println("太祖皇帝!"); return;
} if(age < 1){ System.out.println("皇子!"); } System.out.println("呵,凡人!");
//這時候的年齡就正常了,因此可以給成員變量了 this.age = age; }}5.關(guān)于不同包之間的訪問【全限定類名和import語句;兩者都可以達(dá)到相同的效果,不過全限定類名太長了!】。
package aftertheclass;import b.Clazz;public class _71307 { public static void main(String[] args) {
//導(dǎo)包還不算完,還得創(chuàng)建對象后才能訪問成員方法 Clazz myClazz = new Clazz(); myClazz.hello(); //全限定類名后還不算完,還得創(chuàng)建對象后才能訪問成員方法
a.Clazz myclazz = new a.Clazz();//【全限定類名 + 對象名字 = new + 全限定類名】 myclazz.hello(); }}
a包和b包的位置;6.封裝的get/set方法package aftertheclass;public class _71308 { public static void main(String[] args) {
//創(chuàng)建對象 Catt t = new Catt(); Dog d = new Dog(); //利用set方法去給私人變量賦值 t.setAge(12);
//打印私人成員變量 System.out.println(t.getAge()); d.setAge(13); System.out.println(d.getAge());
//用子類的身份調(diào)用父類的屬性 t.eat(); t.shout(); t.catchMath(); d.watchDoor();
}}class Animal1{ private int age; private String name; //get方法因?yàn)槭窍蛲饨鐐鬟f信息,所以要有return也要有返回值類型
public String getName(){ return name; } //set方法因?yàn)槭菍Τ蓡T變量賦值,因此不要有返回值類型也不要有return,要有輸入類型,以完成賦值。
public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age;
} public int getAge(){ return age; } public void shout(){ System.out.println("爺會叫!");
} public void eat(){ System.out.println("爺會吃!"); }}class Catt extends Animal1{ public void catchMath(){
System.out.println("catch!"); }}class Dog extends Animal1{ public void watchDoor(){ System.out.println("watch!");
}}7.super關(guān)鍵字與繼承package aftertheclass;public class extend01 { public static void main(String[] args) {
Son s = new Son(1);//main 初始化后就是son引用的初始化,去找單參構(gòu)造器① //從單參回來下一個就是他,此乃引用的結(jié)果,用的是son中成員變量 System.out.println(s.a);
//創(chuàng)建對象,轉(zhuǎn)到單參② Father f = new Son(32); System.out.println(f.a); //包括去找哪個無參,都是對象說了算
Grandfather gf = new Son(221); //以下屬于是方法的結(jié)果,由創(chuàng)建方法決定,幾個方法都是son s.test(); f.test();
gf.test(); }}//類1class Grandfather{int gA = 10;//從最上方的顯式賦值開始賦值肯定是家族一起賦值int a = 10;//由于無參構(gòu)造里面也有隱含的super,所以到了太祖皇帝無參構(gòu)造①
public Grandfather() { } public void testGrand(){ System.out.println("高祖皇帝");}public void test(){
System.out.println("高祖");} public Grandfather(int gA, int a) { this.gA = gA; this.a = a;
}}//類2class Father extends Grandfather{int fA = 3 ;//來到了先皇處,依然是顯式賦值int a = 4;//來到了father無參構(gòu)造① public Father() {
} public void testFather(){ System.out.println("先皇");}public void test(){ System.out.println("先帝");
} public Father(int fA, int a) { this.fA = fA; this.a = a; } public Father(int gA, int a, int fA, int a1) {
super(gA, a); this.fA = fA; this.a = a1; }}//類3class Son extends Father{int sA = 112;//來到圣上處,顯式賦值
public void testSon(){ System.out.println("當(dāng)今皇上");}public void test(){ System.out.println("圣上");
} public Son(int sA) { super();//來到單參,因此啟用父類單參構(gòu)造,直接碰見super①②再走一遍 this.sA = sA;//super執(zhí)行完了,該輪到this關(guān)鍵字了。
① } public Son(int fA, int a, int sA) { super(fA, a); this.sA = sA; } public Son(int gA, int a, int fA, int a1, int sA) {
super(gA, a, fA, a1); this.sA = sA; }}yi定要注意,創(chuàng)建對象時引用和對象之間誰能決定轉(zhuǎn)向哪個成員變量或成員方法