1.类与对象
类是一个抽象的模板;
对象是类的一个具体的实例;
2.方法
方法的定义:修饰符,返回类型,方法名,参数列表,异常抛出,返回值。
方法的调用:静态方法,非静态方法,形参和实参,值传递和引用传递,this关键字。
public class Demo01 {
public static void main(String[] args) throws Exception {
int c = 1;
int d = 2;
//c,d实际参数的值传入a,b形式参数
//直接调用静态方法
int add = add(c, d);
System.out.println(add);
//非静态方法需要new一个对象
Demo01 s = new Demo01();
s.test();
}
//静态方法
//a,b形式参数
public static int add(int a, int b){
//返回值
return a+b;
}
//非静态方法
//throws Exception,异常抛出
public void test()throws Exception {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
for (int k = 1; k < i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
3.对象的引用
引用类型:除8种基本类型以外的都是引用类型。
对象时通过引用来操作的:栈—>堆
4.属性
字段Field 成员变量。
默认初始化:
数字:0/0.0
char: u000
boolean: false
引用:null
初始化:
修饰符 属性类型 属性名 = 属性值
5.对象的创建和使用
- 必须使用new关键字创建对象,构造器 :Person zhangsan = new Person();
- 对象的属性 :zhangsan.name
- 对象的方法 :zhangsan.speak()
6.类:
静态的属性 属性
动态的行为 方法