1.
package llll; public class student { String name=new String("该学生还未命名"); double achievement; public student(){ } public student(String str,double achievement){ this.name=str; this.achievement=achievement; } void set(String str,double achievement){ this.name=str; this.achievement=achievement; } void get(){
System.out.println("该学生姓名为:"+name); System.out.println("该学生成绩为:"+achievement); } }
package llll; public class t { public static void main(String args[]){ student A=new student(); A.set(" 小明",90); A.get(); student B=new student("小王",60); B.get(); } }
2
package finish; public class ttt { public void Test() { System.out.println("无参的调用方法被调用了"); } } package finish; public class b extends ttt { public static void main(String[] args) { ttt z=new ttt(); z.Test(); } }
3.
package finish; public class ttt { String name; String color; public static void main(String[] args) { ttt c1=new ttt("benchi","红色"); ttt c2=new ttt("dazhong","黑色"); } public ttt(String name1, String color1) { name = name1; color = color1; run(); } public void run() { System.out.println(color + "的" + name + "正在跑。"); } }
4.
package finish; public class ttt { public static void main(String[] args) { get(9,13); } public static void get(int i, int j) { System.out.println(i + j); } }
5.
封装就是:隐藏对象的属性和实现细节,仅对外提供公共访问方式。
封装的好处:
重用;
不必关心具体的实现;
面向对象三大特征之一;
具有安全性
set访问器是给属性赋值的,get访问器是取得属性值的