zoukankan      html  css  js  c++  java
  • java 类初识

    一、定义

    成员变量

    成员方法

    注意:

    1、成员变量有默认值,是全局变量

    2、成员方法,不需要使用static

    3、成员变量的默认值

    整型 0

    浮点型 0.0

    引用数据类型 null

    二、使用

    1、导包

    2、实例化

    3、使用

    注意:

    1、同一目录下的类不需要导包

    2、实例化

    类 对象 = new 类();

    ps: 导包是 import 路径  感觉没有python的导包人性化

    例子

     1 package cn.wt.day06;
     2 
     3 public class Student {
     4     // 成员变量
     5     String name;
     6     int age;
     7 
     8     // 成员方法
     9     public void eat(String name){
    10         System.out.println(name + "吃饭");
    11     }
    12     // 成员方法
    13     public int score(int a, int b){
    14         return a+b;
    15     }
    16 }
    Student.java
     1 package cn.wt.day06;
     2 
     3 public class Demon01 {
     4     public static void main(String[] args) {
     5         // 实例化
     6         Student stu = new Student();
     7         // 赋值
     8         stu.name = "tom";
     9         stu.age = 9999;
    10         System.out.println(stu.name);
    11         System.out.println(stu.age);
    12         // 调用方法
    13         stu.eat("耗子");
    14         int isScore = stu.score(90, 69);
    15         System.out.println(isScore);
    16     }
    17 }
    Demon01

    三、参数、返回值

    对象可以作为参数和返回值

    注意:作为参数和返回值,传递的是内存地址

    1、参数

     1 package cn.wt.day06;
     2 
     3 public class Demon02 {
     4     public static void main(String[] args) {
     5         Student stu = new Student();
     6         System.out.println(stu);
     7         // 对象 做为 参数, 注意传递的是地址
     8         int res = isSum(stu, 100, 200);
     9         System.out.println(res);
    10     }
    11 
    12     public static int isSum(Student stu, int a, int b){
    13         System.out.println(stu);
    14         int result = stu.score(a, b);
    15         return result;
    16     }
    17 }
    Demon02

    2、返回值

     1 package cn.wt.day06;
     2 
     3 public class Demon03 {
     4     public static void main(String[] args) {
     5         Student s1 = new Student();
     6         // 类 做为 参数 和 返回值
     7         Student res = getStudent(s1);
     8         System.out.println(res);
     9         System.out.println(res.name);
    10     }
    11 
    12     public static Student getStudent(Student stu){
    13         stu.name = "海贼王";
    14         return stu;
    15     }
    16 }
    Demon03

    类的实例化对象做为方法的参数和返回值,和Array 做为方法的参数和返回值一样,传递的都是地址

    四、成员变量与局部变量的区别

    1、位置

    成员变量: 类中,方法外

    局部变量:方法内

    2、作用域

    成员变量:整个类

    局部变量:局部作用域

    3、默认值(不赋值的情况下)

    成员变量:有默认值

    局部变量:会报错

  • 相关阅读:
    【支付宝支付】手机网页内 支付宝支付实现过程
    【微信支付】微信端的手机网页支付 开发流程
    【Linux】Linux下 环境变量/etc/profile、/etc/bashrc、~/.bashrc的区别【转】
    SLF4J 和 Logback 在 Maven 项目中的使用方法
    打造Spring Cloud构建微服务架构的最全资料
    WebApi安全性 使用TOKEN+签名验证
    <meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" />的意义
    request.getParameter(“参数名”) 中文乱码解决方法
    Mysql的timestamp类型,自动记录数据的更新时间
    根据身份证号,取得行政区划的Javascript实现
  • 原文地址:https://www.cnblogs.com/wt7018/p/12178401.html
Copyright © 2011-2022 走看看