@
// Demo01 类
public class Demo01 {
// main 方法
public static void main(String[] args) {
}
/*
修饰符 返回值类型 方法名(...){
// 方法体
return 返回值;
}
*/
// return 结束方法,返回一个结果。
public String sayHello(){
return "hello,world!";
}
public int max (int a,int b){
return a > b ? a : b;// 三元运算符
}
}
// 学生类
public class Student {
// 非静态方法
public void say(){
System.out.println("学生说话了");
}
}
public class Demo02 {
public static void main(String[] args) {
// 实例化这个类 new
// 对象类型 对象名 = 对象值;
Student student = new Student();
student.say();
}
// 静态方法 和类一起加载的
public static void a(){
}
// 非静态方法 类实例化后 才存在
public void b(){
}
}
public class Demo03 {
public static void main(String[] args) {
// 实际参数和形式参数的类型要对应!
Demo03.add(1,2);// 静态方法 可用类名直接调用
}
public static int add(int a,int b){
return a+b;
}
}
// 引用传递:对象,本质还是值传递
public class Demo05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
Demo05.change(person);
System.out.println(person.name);
}
public static void change(Person person){
// person 是一个对象:指向的--->Person person = new Person();这是一个具体的人,可以改变属性!
person.name = "Slimety";
}
}
// 定义一个Person类,有一个属性:name
class Person{
String name; // null
}
// 值传递
public class Demo04 {
public static void main(String[] args) {
int a = 1;
System.out.println(a);
Demo04.change(a);
System.out.println(a);
}
// 返回值为空
public static void change(int a){
a=10;
}
}
使用new关键字创建对象
使用new关键字创建的时候,出了分配内存空间之外,还会给创建好的对象进行默认的初始化以及对类中构造器的调用。
public class Student {
// 属性:字段
String name; // null
int age; // 0
// 方法
public void study(){
System.out.println(this.name+"在学习");
}
}
public class Application {
public static void main(String[] args) {
// 类:抽象化,实例化
// 类实例化后会返回一个自己的对象!
// xiaoming对象就是一个Student类的具体实例!
Student xiaoming = new Student();
xiaoming.name = "小明";
xiaoming.age = 3;
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
}
}
类中的构造器也称为构造方法,是在进行创建对象的时候必须要调用的。并且构造器有以下两个特点:
构造器必须要掌握
public class Person {
// 一个类即使什么都不写,他也会存在一个方法
// 显式的定义构造器
String name;
// 实例化初始化
// 1.使用new关键字,必须要有构造器
// 无参构造
public Person(){
this.name = "slimety";
}
// 有参构造: 一旦定义了有参构造,无参构造必须显式定义
public Person(String name){
this.name = name;
}
}
public class Application {
public static void main(String[] args) {
// new 实例化了一个对象
Person person = new Person();
System.out.println(person.name);
}
}
// 类 private:私有
public class Student {
// 属性私有
private String name; // 姓名
private int id;// 学号
private char sex;// 性别
private int age;
// 提供一些可以操作name属性的方法!
// 提供一些public的get、set方法
// get 获得这个数据
public String getName(){
return this.name;
}
// set 给这个数据设置值
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){
this.age = 3;
}else{
this.age = age;
}
}
}
/*
1.提高程序的安全性,保护数据
2.隐藏代码的安全细节
3.统一接口
4.系统可维护增加了
*/
// 一个项目应该只存在一个main方法
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("slimety");
System.out.println(s1.getName());
s1.setAge(999);
System.out.println(s1.getAge());
}
}
// 在Java中,所有的类,都默认直接或间接继承object
// Person 人 父类
public class Person {
private int money = 10_0000_0000;
public void say(){
System.out.println("说了一句话");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
// 派生类,子类
// 子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person{
}
// 派生类,子类
public class Teacher extends Person{
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
}
}
public class Person {
public Person() {
System.out.println("Person无参执行了");
}
// public Person(String name) {
// System.out.println("Person无参执行了");
// }
protected String name = "slimety";
public void print(){
System.out.println("Person");
}
}
public class Student extends Person{
private String name = "ty";
public Student() {
// 隐藏代码:调用了父类的无参构造
super();// 调用父类的构造器,必须要在子类构造器的第一行
System.out.println("Student无参执行了");
}
// public Student() {
// super("sliemty");// 当父类只定义了有参构造,就必须显式调用有参super()
// System.out.println("Student无参执行了");
// }
public void print(){
System.out.println("Student");
}
public void test1(){
print();// Student
this.print();// Student
super.print();// Person
}
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test1();
}
}
public class B {
public static void test(){
System.out.println("B=>test()");
}
}
public class A extends B{
public static void test(){
System.out.println("A=>test()");
}
}
public class Application {
public static void main(String[] args) {
// 静态方法:方法的调用只和左边,定义的数据类型有关
A a = new A();
a.test();
// 父类的引用指向了子类
B b = new A();
b.test();
}
}
代码运行结果如图,此时A类和B类的test()方法均为静态方法
public class B {
public void test(){
System.out.println("B=>test()");
}
}
public class A extends B{
@Override // 重写
public void test() {
System.out.println("A=>test()");
}
}
public class Application {
public static void main(String[] args) {
A a = new A();
a.test();
// 父类的引用指向了子类
B b = new A();// 子类重写了父类的方法
b.test();
}
}
代码运行结果如图,此时A类和B类的test()方法均为非静态方法
因为静态方法是类的方法,而非静态是对象的方法,有static时,b调用了B类的方法,因为b是用b类定义的,没有static时,b调用的是对象的方法,而b是用A类new的,即b是A new出来的对象,因此调用了A的方法。
方法重写总结:
注:方法重写只能针对非静态,非final,非构造方法。
感谢B站狂神说,让我学到这些
希望我的教程能帮助到你,如有不足之处,希望大佬指出
原文:https://www.cnblogs.com/slimety/p/14649716.html