方法的定义
//mian方法
public static void main(String[] args) {
}
/*
修饰符 返回值 方法名(){
方法体
返回值 return;
}
*/
public String sayHello(){
return "hello.world";
}
public void print(){
return;//结束方法
}
public int max(int a, int b){
return a > b ? a : b;
}
//数组下标越界 Arrayindexoutofbounds
public void readFile(String file)throws IOException{
}
方法的调用
package com.oop.Demo02;
public class Student {
//属性 :字段
String name; //null
int age; //0
//方法
public void study(){
System.out.println(this.name+"在学习");
}
}
/**
* //类;抽象的 实例化成对象
* //类实例化后会返回一个自己的对象
* //Student对象是一个Student类的具体实现
* Student xiaoming = new Student();
* Student xh = new Student();
*
* xiaoming.name = "小明";
* xiaoming.age = 3;
* System.out.println(xiaoming.name);
* System.out.println(xiaoming.age);
*
* xh.name = "小红";
* xh.age = 3;
* System.out.println(xh.name);
* System.out.println(xh.age);
*
*
*/
package com.oop.Demo02;
public class Person {
//一个类即使什么也不写,他也会存在一个方法
//显示的定义构造器
String name;
int age;
//1.使用new关键字,本质上是在调用构造器
//2.用来初始化值
public Person() {
}
//一旦定义了一个有参构造,无参就必须显示定义
public Person(String name) {
this.name = name;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
/*
//new实例化了一个对象
Person person = new Person();
Person person1 = new Person("张三");
System.out.println(person1.name);
构造器:
1.和类名相同
2.没有返回值
作用
1.new本质在调用构造方法
2。初始化对象的值
注意点:
1.定义有参构造之后,如果使用无参构造,显示的定义一个无参的构造
ALT+INSERT
this. =
*/
}
类与对象
package com.oop.Demo03;
public class Pet {
public String name;
public int age;
public void shot(){
System.out.println("叫了一声");
}
}
/*
Pet dog = new Pet();
dog.name = "旺财";
dog.age = 3;
Pet cat = new Pet();
*/
package com.oop.Demo03;
import com.oop.Demo03.Pet;
public class Application {
public static void main(String[] args) {
/*
1.类与对象
类是一个模板:抽象,对象是一个具体的实例
2.方法
定义.调用!
3.对象的引用
引用类型:基本变量(8)
对象是通过引用来操作的:栈--堆
4.属性:字段Field 成员变量
默认初始化
数字:0 0.0
char:u0000
boolean:false
引用:null
修饰符 属性 属性名=属性值!
5.对象的创建与使用
-必须使用new 关键字创造对象,构造器 Person person = newPerson();
-对象的属性 person.name
-对象的方法 person.sleep()
6.类
静态的属性 属性
动态的方法 方法
java 封装 继承 多态
*/
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W28R5X2V-1618759586627)(C:\Users\熊志勇\AppData\Roaming\Typora\typora-user-images\image-20210418231822468.png)]
封装 属性私有 get set
package com.oop.Demo04;
//类 private :私有
public class Student {
//属性私有
private String name; //名字
private int id; //学号
private char sex; //性别
private int age; //年龄
//提供一些可以操作这个属性的方法!
//提供一些public 的get.set 方法
//获得这个数据
public String getName() {
return name;
}
//给这数据设置值
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>120||age<0){//不合法
age=3;
}else{
this.age = age;
}
}
/*
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统的可维护性
Student s1 = new Student();
s1.setName("zhangsan");
System.out.println(s1.getName());
s1.setAge(999);//不合法
//方法名 参数列表
System.out.println(s1.getAge());
s1.setAge(60);
System.out.println(s1.getAge());
*/
}
继承
package com.oop.Demo05;
//在java中,所有的类都默认直接或间接继承Object
//person 人 父类
public class Person {
//public
//protected
//default
//private
private int money = 10;
public static void say(){
System.out.println("说话");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
package com.oop.Demo05;
//学生is人 派生类 子类
//子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person{
//ctrl+H 显示继承关系
}
package com.oop.Demo05;
//老师is人
public class Teacher extends Person{
}
package com.oop;
import com.oop.Demo05.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
Student.say();
}
}