JAVA零基础部分笔记
1.封装详解
我们程序要追求”高内聚,低耦合“。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。
封装(数据的隐藏):应禁止直接访问一个对象中的数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。
属性私有:get/set
private:私有
封装的作用:①提高程序的安全性,保护数据 ②隐藏代码的实现细节
③统一接口 ④系统可维护增加
package Demo04;
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("周杰伦");
System.out.println(s1.getName());
s1.setAge(37);
System.out.println(s1.getAge());
}
}
package Demo04;
//类 private :私有
public class Student {
//属性私有
private String name;//名字
private int age;//年龄
//提供一些可以操作这个属性的方法
//提供一些public的get、set方法
//get获得这个数据
//set 给这个数据设置值
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 120 || age < 0) {
System.out.println("不合法");
this.age=3;
} else {
this.age = age;
}
}
}
2.什么是继承
1.继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模.
2.extends的意思是”扩展”。子类是父类的扩展.
3.Java中类只有单继承,没有多继承.
//子类继承了父类,就会拥有父类的全部方法!
package Demo04;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
System.out.println(student.money);
}
}
package Demo04;
//学生 is 人 派生类 子类
public class Student extends Person{
//继承了父类全部方法
}
package Demo04;
//在java中 所有的类都默认继承object类
// Person 人:父类
public class Person {
public int money = 100;
public void say(){
System.out.println("说了一句话");
}
}
3. Super详解
1.super调用父类的构造方法,必须在构造父类方法的第一个
2.super必须只能出现在子类的方法或者构造方法中
3.super和this不能同时调用构造方法
4.私有的东西无法被继承
=============================
和this对比:
代表的对象不同:this:本身调用者这个对象 super:代表父类对象的应用
前提:this:没有继承也可以使用 super:只能在继承条件下才可以使用
构造方法:this():本类的构造 super:父类的构造
1.
public class Application {
public static void main(String[] args) {
Student student = new Student();
}
}
=========================
//父类
//在java中 所有的类都默认继承object类
// Person 人:父类
public class Person{
public Person(){
System.out.println("Person也无参执行了");
}
}
=========================
//学生 is 人 派生类 子类
public class Student extends Person{
//继承了父类全部方法
public Student(){
//隐藏代码:调用了父类的无参构造
super();
System.out.println("Student也无参执行了");
}
}
结果:Person也无参执行了
Student也无参执行了
2.
public class MyTest {
public static void main(String[] args) {
new Cat(); //①
}
}
//父类,Animal类
class Animal {
//构造函数
public Animal() {
super();
System.out.println("1:Animal类的无参数构造函数执行");
}
public Animal(int i) {//⑥
super();
System.out.println("2:Animal类的有int参数构造函数执行");//⑦
}
}
//子类,Cat类
class Cat extends Animal{
//构造函数
public Cat() {//②
this("");//③ 此处有引号 所以为字符串
System.out.println("3:Cat类的无参数构造函数执行");//⑨
}
public Cat(String str) {//④
super(5);//⑤
System.out.println("4:Cat类的有String参数构造函数执行");//⑧
}
}
运行结果:2:Animal类的有int参数构造函数执行
4:Cat类的有String参数构造函数执行
3:Cat类的无参数构造函数执行
3.
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("刘德华");
}
}
================================
//父类
public class Person{
protected String name ="周杰伦";
}
================================
//子类
public class Student extends Person{
private String name = "陈奕迅";
public void test(String name){
System.out.println(name);//传递的参数
System.out.println(this.name);//本类中
System.out.println(super.name);//父类中
}
}
结果:刘德华
陈奕迅
周杰伦