第一章 构造器
构造器是一个java用来产生对象的特殊方法。称之为构造器
声明方法和注意事项:
public 类名(){
//语句
}
//构造器方法名称和类名相同 首字母一定大写
//没有返回值 不需要写明返回类型
//可以带参构造
//在类中如果没有构造器 默认存在无参构造器
//构造器可以重载
public class Children {
String name;
int age;
int weight;
char gender;
public Children() {
}
public Children(String yName) {
name = yName;
}
第二章 static关键词
static意思为 静态的 共享的
static的使用:
在static中使用非static修饰的
public class StaticTest {
static int num = 10;//增加static修饰 不建议使用
public static void main(String[] args) {
System.out.println(num);
}
}
StaticTest t = new StaticTest();//创建对象使用
System.out.println(num);
在非static中使用static修饰的
public class StaticTest {
static int num = 10;
public static void main(String[] args) {
StaticTest t = new StaticTest();
t.method();
}
public void method() {
System.out.println(num);//同一个类中 直接使用变量或者方法名
}
}
//非同类的 类名.变量或方法
public class Test02 {
public static void main(String[] args) {
System.out.println(StaticTest.num);
}
}
第三章 this和super关键词
this: 指的是当前对象 用法有两种 一种是 this. 一种是 this()
this.:一般可以省略 当有相同名字需要区分的时候使用
public Dog(String yName) {
this.name = yName;//可省略
}
public Dog(String name) {
this.name = name;//不可
}
this():用来实现构造器之间的互相调用。
public Dog() {
this("101");//通过this()调用了带一个参数的构造器
}
public Dog(String name) {
this.name = name;
}
super: 指的是父类对象 用法和this一样
对于所有的子类而言。在子类的构造器的第一行都存在super();
this()和super()不能共存。当构造器中存在super,就不能显式编写this();如果构造器中存在
this(),那么构造器中的默认super()空构造器就不存在了。
父类的构造器只会被调用一次。
第四章 继承
意思就是子承父业。用来构造类和类之间的关系。可以继承父类属性和方法
public class fa{
int aaa;
System.out.println("aaa");
}
public class son extends fa{
int bbb;
System.out.println("bbb");
}
public class Test01 {
public static void main(String[] args) {
son c = new son();
c.aaa=1;
System.out.println(c.aaa);
}
}
// son 没有aaa 但是可以操作aaa 因为fa被son继承了 所以能使用fa中aaa
在java中一旦发生继承关系之后,子类就会拥有父类中的属性以及方法。子类无需自己再去声明。达到了代码复用性的要求。
继承只能是单向单线的