当前位置: 首页 > news >正文

java类与对象,用程序解释

//第一个
1
public class Cardemo{ 2 public static void main(String[] args){ 3 Car carwind = new Car(60,"red","west"); 4 carwind.speed = 60; 5 carwind.color = "red"; 6 carwind.direction = "west"; 7 8 System.out.println("The speed of car is " + carwind.speed); 9 System.out.println("The color of car is " + carwind.color); 10 System.out.println("The direction of car is " + carwind.direction); 11 } 12 } 13 class Car{ 14 //定义四个成员变量(属性) 15 int speed; 16 String color; 17 String name; 18 String direction; 19 }

第二个

public class Cardemo{
    public static void main(String[] args){
        Car carwind = new Car(60,"red","west"); //创建一个Car类的引用
        carwind.speed = 60;
        carwind.color = "red";
        carwind.direction = "west";
        
        System.out.println("The speed of car is " + carwind.speed);
        System.out.println("The color of car is " + carwind.color);
        System.out.println("The direction of car is " + carwind.direction);
        carwind.drivecar();
        carwind.speedup(5);
        System.out.println("The speed of car is " + carwind.speed);
        
    }
}
class Car{
    //定义四个成员变量(属性)
    int speed;
    String color;
    String name;
    String direction;
    
    Car(int speed, String color, String direction){
        this.speed = speed;
        this.color = color;
        this.direction = direction;
    }
    
    public void drivecar(){
        System.out.println("here we go!");
    }
    public void speedup(int aspeed){
        this.speed = speed + aspeed;
    }
}

 

转载于:https://www.cnblogs.com/xiaolei-meow/p/6535668.html

相关文章:

  • 代码生成器修改备注
  • 常用的字符串方法 String ;
  • 在SQL Server 2008上实现资源的负载均衡
  • 【转】angular基本概念的认识与实战
  • SQL Server中未公布的扩展存储过程
  • 利用jQuery中live为动态生成Dom添加datepicker效果
  • ABAP clear,refresh,free清空内表的区别
  • 作为软件开发人员应该知道的最基本的东西
  • 让IE6、IE7、IE8、IE9、IE10、IE11支持Bootstrap的解决方法
  • 虚拟服务器负载均衡实现方法
  • 架构重构改善既有代码的设计
  • 详解SQL Server中创建数据仓库已分区表
  • SQL server故障转移和负载均衡
  • [LeetCode]284. Peeking Iterator(C++,类,暴力)
  • SQL Server——海量数据库的查询优化及分页算法方案
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • angular学习第一篇-----环境搭建
  • CentOS从零开始部署Nodejs项目
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • es6--symbol
  • Fastjson的基本使用方法大全
  • Java基本数据类型之Number
  • Laravel核心解读--Facades
  • leetcode98. Validate Binary Search Tree
  • Mac 鼠须管 Rime 输入法 安装五笔输入法 教程
  • Python打包系统简单入门
  • Swift 中的尾递归和蹦床
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 产品三维模型在线预览
  • 将回调地狱按在地上摩擦的Promise
  • 聚簇索引和非聚簇索引
  • 力扣(LeetCode)22
  • 深入浏览器事件循环的本质
  • 微服务入门【系列视频课程】
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • # Maven错误Error executing Maven
  • $refs 、$nextTic、动态组件、name的使用
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (04)odoo视图操作
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (js)循环条件满足时终止循环
  • (MATLAB)第五章-矩阵运算
  • (Oracle)SQL优化技巧(一):分页查询
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (一)Java算法:二分查找
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (轉)JSON.stringify 语法实例讲解
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .cn根服务器被攻击之后
  • .gitattributes 文件
  • .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖
  • .Net中的集合
  • .Net转Java自学之路—SpringMVC框架篇六(异常处理)
  • @DependsOn:解析 Spring 中的依赖关系之艺术