|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 糖逗 于 2020-8-17 16:26 编辑
学习视频:https://www.bilibili.com/video/BV17J411G72L?p=20
125-142
- /*
- *面向对象思想 JAVA语言 程序的最小单位是类
- *把复杂的业务逻辑/功能中先抽取出“类”,然后再考虑这个类中包含什么属性和方法
- *面向对象是把数据和功能(行为)合起来的。关注怎么做
- *面向过程思想 C语言 程序的最小单位是函数
- *把大的功能分解成一个一个小的功能
- *面向过程是把数据与功能是分开的。关注谁做
- */
- /*
- *类:一类具有相同特征的事务的抽象描述
- *类和对象的关系:类是众多对象的抽象描述,类是对象的设计图,创建对象的模板
- *对象是类的个体,具体的实例
- */
- /*
- *类的提取:名词提取,转换成现实世界中的事物的类别
- *类的设计:类的成员设计 1.属性(数据特征)2.方法:行为特征,功能特征
- *3.构造器:创建对象时用 4.代码块:在类的初始化和对象初始化时用
- *5.内部类:在某个大的事物中,包含了一个独立的小的事物的类别,而且一般这个小的事物类别只为大的事物服务时
- */
- class Body{
- //属性
- double weight;
- char gender;
- //方法
- void eat(){
- System.out.println("吃饭");
- }
- void sleep(){
- System.out.println("睡觉");
- }
- //构造器
- Body(){
- }
- Body(double weight, char gender){
- this.weight = weight;
- this.gender = gender;
- }
- //代码块
- {
- System.out.println("非静态代码块");
- }
- static{
- System.out.println("静态代码块");
- }
- //内部类
- class Heart{
- public void beat(){
- System.out.println("砰砰跳");
- }
- }
- }
- /*
- *1.类的声明:
- *【修饰符】 class 类名{
- *
- *}
- *如果一个类是public,那么源文件名必须与类名相同!!!!!
- *2.类的第一类成员:属性
- *(1)属性的声明 【修饰符】 数据类型 属性名;
- * (2)类名每个单词的首字母大写(命名规则)
- *(3)对象名/变量名:从第二个单词开始首字母大写
- */
- //声明类
- public class java07{
- public static void main(String[] args){
- //创建对象
- Person p = new Person();
- //为p对象的属性赋值
- p.name = "张三";
- p.age = 23;
- p.gender = '男';
- //访问某个对象的属性
- System.out.println(p.gender);
- }
- }
- //声明类
- class Person{
- //属性列表
- String name;
- int age;
- char gender;
- }
- /*
- *属性的特点:
- *1.有默认值,原则和数组元素的默认值一样
- *(1)基本数据类型:
- *byte short int long : 0
- *float double : 0.0
- *boolean : false
- *char : \u0000
- *(2)引用数据类型:null
- *例如:String等
- *2.每一个对象的属性值是独立的
- */
- class test07_01{
- public static void main(String[] args){
- //创建一个动物的对象
- Animal al = new Animal();
- //
- }
- }
- class Animal{
- String name;
- int legs;
- }
- /*
- *类的成员之二:方法
- *方法(method)又称为函数(function)
- *方法是代表一个独立的可复用的功能
- *声明方法的好处:
- *(1)代码的复用
- *(2)简化代码量
- *1.声明一个方法
- *【修饰符】 返回值的类型 方法名(形参列表){
- * 方法体:实现代码的功能
- *}
- *2.调用方法
- *3.命名规范:和变量名一样,胸第二个单词开始首字母大写
- */
- class test07_02{
- public static void main(String[] args){
- printRectangle();
- }
- //public static 是修饰符
- //void 是返回类型,它是一种特殊的返回值类型,表示无返回
- public static void printRectangle(){
- for(int i = 1; i <= 5; i ++){
- for(int j = 1; j <= 10; j++){
- System.out.print("*" + " ");
- }
- System.out.println();
- }
- }
- }
- /*
- *无参无返回值的方法
- *调用:(1)在本类中,同一个级别,直接调用
- *(2)在其他类中,需要通过对象名.方法来调用
- */
- class test07_03{
- public static void main(String[] args){
- //创建对象
- Student s1 = new Student();
- s1.name = "小红";
- s1.age = 21;
- s1.score = 98;
- //通过对象去调用方法
- s1.printInfo();
- }
- }
- class Student{
- //属性
- String name;
- int age;
- int score;
- //方法
- void printInfo(){
- System.out.println("姓名" + name + "年龄" + age);
- }
- }
- /*
- *无参有返回值的方法
- *
- */
- class test07_04{
- public static void main(String[] args){
- Account a1 = new Account();
- a1.balance = 10000;
- a1.printInfo();
- double d = a1.getMonthlyRate();
- System.out.println(d);
- }
- }
- class Account{
- String id;
- double balance;
- double rate = 0.035;
- void printInfo(){
- System.out.println("账户" + id);
- }
- double getMonthlyRate(){
- return rate / 12;
- }
- }
- /*
- *有参无返回值的方法
- *形参列表:声明时()中
- *实参列表:调用时()中
- */
- class test07_05{
- public static void main(String[] args){
- GrahicTools tools = new GrahicTools();
- tools.printRectangle();
- tools.printRectangleN(4, 5);
- }
- }
- class GrahicTools{
- void printRectangle(){
- for(int i = 1; i <= 5; i ++){
- for(int j = 1; j <= 10; j++){
- System.out.print("*" + " ");
- }
- System.out.println();
- }
- }
- void printRectangleN(int n, int m){
- for(int i = 1; i <= n; i ++){
- for(int j = 1; j <= m; j++){
- System.out.print("*" + " ");
- }
- System.out.println();
- }
- }
- }
- /*
- *有参有返回值的方法
- */
- class test07_06{
- public static void main(String[] args){
- MyMathTools tools = new MyMathTools();
- int m = tools.max(4, 6);
- System.out.println(m);
- int n = tools.threeMax(5, 3, 2);
- System.out.println(n);
- }
- }
- class MyMathTools{
- int max(int a, int b){
- return a > b ? a : b;
- }
- int threeMax(int a, int b, int c){
- return max(max(a, b), c);
- }
- }
复制代码 |
-
类的存储
评分
-
查看全部评分
|