回顾方法及加深
- 方法的定义
- 修饰符
- 返回类型
- break:跳出switch,结束循环和return的区别
- 方法名:注意命名原则 见名知意
- 参数列表:(参数类型 ,参数名)
- 异常输出:疑问
- 方法的调用:递归
- 静态方法
- 非静态方法
- 形参和实参
- 值传递和引用传递
- this关键字
静态方法和非静态方法
静态方法 static
package oop;
public class Demo01 {
public static void main(String[] args) {
//实例化这个类 new
//对象类型 对象名 = 对象值;
Student01.whisper();
}
}
package oop;
public class Student01 {
//静态方法
public static void whisper(){
System.out.println("jjking");
}
}
非静态方法
package oop;
public class Demo01 {
public static void main(String[] args) {
//实例化这个类 new
//对象类型 对象名 = 对象值;
student student = new student();
student.say();
}
}
package oop;
//学生类
public class student {
//非静态方法
public void say(){
System.out.println("111");
}
}
a无法调用b;
package oop;
public class Demo02 {
public static void main(String[] args) {
}
//static和类一起加载的 无法调用不存在的方法
public static void a(){
//b();
}
//类实例化 之后才存在
public void b(){
a();
}
}
形参和实参
静态
package oop;
public class Demo03 {
public static void main(String[] args) {
int add = add(1, 3);
System.out.println(add);
//int add = Demo03.add(1, 3);
//System.out.println(add);
}
public static int add(int a,int b){
return a+b;
}
}
非静态
package oop;
public class Demo04{
public static void main(String[] args){
//形式参数和实际参数要对应
//int add = new Demo04().add(1, 3);
//System.out.println(add);
Demo04 demo04 = new Demo04();
int add = demo04.add(1, 3);
System.out.println(add);
}
public int add(int a,int b){
return a+b;
}
}
值传递和引用传递
值传递:
package oop;
//值传递
public class Demo05 {
public static void main(String[] args) {
int a = 1;
System.out.println(a);
Demo05.change(a);
System.out.println(a);
}
//返回值为空
public static void change(int a){
a = 10;
}
}
输出结果:
1
1
Process finished with exit code 0
引用传递:
package oop;
//引用传递:对象,本质还是值传递
public class Demo06 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);//null
change(person);
System.out.println(person.name);
}
public static void change(Person person){
person.name = "zxzx";
}
}
class Person{
String name ;
}
输出结果:
null
zxzx
Process finished with exit code 0
类与对象的创建
给一个类
package oop;
//学生类
public class StudentDemo07 {
//属性;字段
String name;
int age;
//方法
public void study(){
System.out.println(this.name+"在洗澡");
}
}
//类与对象的创建
package oop;
//一个项目应该只存在一个main方法
public class Application {
public static void main(String[] args) {
//类:抽象的 要对其实例化
//类实例化后会返回一个自己的对象;
//studentDemo07对象就是一个StudentDemo07的具体实例!
//StudentDemo07 studentDemo07 = new StudentDemo07();
StudentDemo07 zx = new StudentDemo07();
StudentDemo07 xz = new StudentDemo07();
zx.name = "销虎";
zx.age = 21;
System.out.println(zx.name);
System.out.println(zx.age);
xz.name = "污渍";
xz.age = 22 ;
System.out.println(xz.name);
System.out.println(xz.age);
zx.study();
xz.study();
}
输出结果:
销户
21
污渍
22
销户在洗澡
污渍在洗澡
Process finished with exit code 0
以类的形式组织代码,以对象的形式封装数据。
构造器详解
package oop;
public class Application01 {
public static void main(String[] args) {
Person01 person01 = new Person01("zxzxzx");
System.out.println(person01.name);
}
}
package oop;
public class Person01 {
//构造器:1、必须和类的名字相同;2、必须没有返回类型,也不能写void;
//一个类什么都不写,也会存在一个方法;
//显示的定义构造器
String name;
//作用1:实例化初始值
//作用2:使用new关键字本质是调用构造器
public Person01(){
//无参构造器
//this.name = "zx";
}
//有参构造 一旦定义了有参构造 无参构造必须显示定义
public Person01(String name){
this.name = name;
}
}
package oop;
public class Person02 {
String name;
int age;
public Person02(String name) {
this.name = name;
}
public Person02(int age) {
this.age = age;
}
public Person02() {
}
public Person02(String name, int age) {
this.name = name;
this.age = age;
}
}
//快捷键 alt+insert 笔记本电脑fn+alt+delete
/*
构造器:
1、和类名相同
2、没有返回值
作用:
1、new本质是在调用构造器
2、初始化对象的值
注意点:
1、定义有参构造之后,如果想使用无参构造,必须要显示的
定义一个构造器
2、快捷键 alt+insert 笔记本电脑fn+alt+delete
*/
构造器:
- 和类名相同
- 没有返回值
作用:
- new本质是在调用构造器
- 初始化对象的值
注意点:
- 定义有参构造之后,如果想使用无参构造,必须要显示的 定义一个构造器
- 快捷键 alt+insert 笔记本电脑fn+alt+delete
创建对象内存分析
package oop;
public class Pet {
String name;
int age;
public void shout(){
System.out.println(this.name+"开始了");
}
}
package oop;
public class Application02 {
public static void main(String[] args) {
Pet dog = new Pet();
dog.name = "wbnq";
dog.age = 3;
dog.shout();
System.out.println(dog.name);
System.out.println(dog.age);
}
}
整理小节
- 类与对象
类是一个模板,对象是一个具体的实例
类只有属性和方法
使用new关键字创造对象
Person zx = new Person();
zx.name;//对象的属性
zx.say();//对象的方法
- 方法的定义,调用(静态和非静态)
- 修饰符 属性类型 属性名 = 属性值