• Java面向对象一


    属性加方法是类

    package com.oop.demo01;
    
    import java.io.IOException;
    
    //Demo01 类
    public class Demo01 {
        //main 方法
        public static void main(String[] args) {
    
        }
    
    
        /*
        修饰符  返回值类型  方法名(...){
            //方法体
            return 返回值;
        }
         */
    
        //return 结束方法,返回一个结果!
        public String sayHello(){
            return "hello world";
        }//return 代表当前方法结束
    
        public int max(int a, int b){
            return a>b ? a : b;//三元运算符
        }
    
        //数组下标越界:Arrayindexoutofbounds
        public void readFile(String file) throws IOException {
    
        }
    
    }
    
    package com.oop.demo01;
    
    public class Demo02 {
    
        //静态方法  static
        //非静态方法
        public static void main(String[] args) {
            //实例化这个类 new
            new Student().say();
            //对象类型  对象名     对象值
            Student student = new Student();
            student.say();
        }
    
        //和类一起加载的
        public static void  a(){
            //b();
        }
    
        //类实例化之后才存在
        public void b(){
    
        }
    }
    
    package com.oop.demo01;
    
    //学生类
    public class Student {
    
        //非静态方法
        public void say(){
            System.out.println("学生说话了");
        }
    }
    
    package com.oop.demo01;
    
    public class Demo03 {
    
        public static void main(String[] args) {
            //实际参数和形式参数的类型要对应!
            int add = new Demo03().add(1,2);
            System.out.println(add);
        }
    
        public int add(int a, int b){
            return a+b;
        }
    }
    
    package com.oop.demo01;
    
    //值传递
    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;
        }
    }
    
    package com.oop.demo01;
    
    //引用传递:对象,本质还是值传递
    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);//tom
        }
        public static void change(Person person) {
            person.name = "tom";
        }
    }
    
    //定义了一个Person类,有一个属性:name
    class Person{
        String name; //null
    }
    

    package com.oop.demo02;
    
    //一个项目应该只存有一个main方法
    public class Application {
        public static void main(String[] args) {
    
            //类:抽象的,要实例化
            //类实例化后会返回一个自己的对象!
            //student对象就是一个Student类的具体实例!
    
            Student student = new Student();
            Student xiaoming = new Student();
            Student xiaohong = new Student();
    
            xiaoming.name="小明";
            xiaoming.age=3;
    
            System.out.println(xiaoming.name);
            System.out.println(xiaoming.age);
    
            xiaohong.name="小红";
            xiaohong.age=3;
    
            System.out.println(xiaohong.name);
            System.out.println(xiaohong.age);
    	xiaohong.study();
        }
    }
    
    package com.oop.demo02;
    
    //学生类
    public class Student {
    
        //属性:字段
        String name;//null
        int age;//0
    
        //方法
        public void study(){
            System.out.println(this.name+"在学习");//this代表当前类
        }
    }
    
    package com.oop.demo02;
    
    //一个项目应该只存有一个main方法
    public class Application {
    
        public static void main(String[] args) {
    
            //new 实例化一个对象
            //输出有参构造器
            Person person = new Person("tom",23);
            System.out.println(person.name);
            System.out.println(person.age);
    
            //输出默认构造器
    //        Person person = new Person();
    //        System.out.println(person.name);
    
    /*
    构造器:
        1. 和类名相同
        2. 没有返回值
    作用:
        1. new 本质在调用构造方法
        2. 初始化对象的值
    注意点:
        1. 定义有参构造之后,如果想使用无参构造,显示的定义一个无参的构造
     */
    
        }
    }
    
    package com.oop.demo02;
    
    //java ---》 class
    public class Person {
    
        //一个类即使什么都不写,它也会存在一个方法
        //显示的定义构造器
    
        String name;
        int age;
    
        //实例化初始值
        //默认的构造器       无参
        //1. 使用new关键字,本质是在调用构造器
        //2. 用来初始化值
        public Person(){
            this.name = "jerry";
        }
    
        //有参构造:一旦定义了有参构造,无参构造就必须显式定义
        //重载           有参
        public Person(String name){
            this.name = name;
        }
    
        //Ait+insert    生成构造器
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
    }
    
  • 相关阅读:
    SpringCloudAlibaba学习笔记-简介
    SpringCloudAlibaba学习笔记-目录
    go语言学习笔记-目录
    go语言学习笔记-配置idea开发go编程语言并配置导入本地包
    go语言学习笔记-Windows10开发环境安装和环境变量配置
    我与阿里云的日常-QuickBI开发教程
    我与阿里云的日常-阿里云帐号注册
    消息队列 RabbitMq(6)高级特性
    消息队列 (5) RabbtMQ SpringBoot整合
    Nginx的安装和使用
  • 原文地址:https://www.cnblogs.com/Notesdata/p/14141946.html
Copyright © 2020-2023  润新知