栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java学习记录9-面向对象

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java学习记录9-面向对象

java学习记录

一、基础二、调用三、规范化四、构造器

一、基础

Demo1.java:

public class Demo1 {
    public static void main(String[] args) {
//        Student.say();
//      对象类型 对象名字  = 对象值
        Student student = new Student();
        student.say();
    }
}

Student.java:

public class Student {
//    public static void main(String[] args) {
//        System.out.println("student");
//    }
//    非静态方法
    public void say() {
        System.out.println("student");
    }
}
二、调用
public class Demo2 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a); // 1
        Demo2.change(a);
        System.out.println(a); // 1
//        ----------------分割线----------------
        Person person = new Person();
        System.out.println(person.name); // null
        Demo2.changeP(person);
        System.out.println(person.name); // 赵四
    }

    public static void change(int a){
        a = 10;
    }
    //        ----------------分割线----------------
    public static void changeP(Person person){
//        person是一个对象:指向=>Person person = new Person();
        person.name = "赵四";
    }
}
class Person{
    String name;
}
三、规范化

Application.java:
‘一个项目应该只有一个main方法’

//一个项目应该只有一个main方法
public class Application {
    public static void main(String[] args) {
//        实例化 学生类
        Student student = new Student();
        student.name = "赋值咯~";
        System.out.println(student.name);
    }
}

Student.java:

//学生类
public class Student {
//    属性:字段
    String name;
    int age;

//    方法
    public void study() {
        System.out.println(this.name);
    }
}
四、构造器

Application.java:

//一个项目应该只有一个main方法
public class Application {
    public static void main(String[] args) {
//        没有传参走public Person() {}
        Person xiaomi = new Person();
//        有传参走public   Person() {(String name) {this.name = name;}
        Person xiaohong = new Person("xiaohong");
        System.out.println(xiaomi.name);
        System.out.println(xiaohong.name);
    }
}

Application.java:

public class Person {
    String name;
    
//    alt+insert快速生成↓
    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/782211.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号