面向对象01

本文详细介绍了Java中方法的概念,包括方法重载的特点及其实现方式,并通过具体实例展示了构造方法的应用及其如何用于对象初始化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.什么是类:相同属性和方法的一组对象的集合

什么是对象:描述客观事物的一个实体,有一组属性和方法构成

方法和属性是什么:方法指的是:对象执行的操作;属性指的是:对象具有的各种特征

3.方法和属性是什么?方法:对象执行的操作

属性:对象具有的各种特征

方法重载: 

 1.方法名一样           

 2.参数列表不一样(参数个数不同或者参数类型不同)

代码:

  1. package com.lenovo.www.entity;  
  2.   
  3. /** 
  4.  * 方法重载 
  5.  *  
  6.  * @author lenovo1 
  7.  * 
  8.  */  
  9. public class Essay {  
  10.     public void writeEssay() {  
  11.         System.out.println("无参方法");  
  12.     }  
  13.   
  14.   
  15.     public void writeEssay(String pencil, String paper) {  
  16.         System.out.println("用纸和笔写文章");  
  17.     }  
  18.   
  19.   
  20.     public void writeEssay(int a, String paper) {  
  21.         System.out.println("用"+a+"支笔写文章");  
  22.     }  
  23.   
  24.   
  25.     public void writeEssay(String a, int b) {  
  26.         System.out.println("用"+b+"张纸写文章");  
  27.     }  
  28.   
  29.   
  30.     public String writeEssay(String computer) {  
  31.         computer = computer+"写文章";  
  32.         return computer;  
  33.     }  
  34. }  

[java]  view plain  copy
  1. package com.lenovo.www.entity;  
  2.   
  3. import java.util.Scanner;  
  4.   
  5. import javax.swing.plaf.synth.SynthSpinnerUI;  
  6.   
  7. /** 
  8.  *  
  9.  * @author lenovo1 
  10.  * 
  11.  */  
  12. public class Test {  
  13.   
  14.     public static void main(String[] args) {  
  15.     Essay essay = new Essay() ;  
  16.     essay.writeEssay(10,"aaa");  
  17.     essay.writeEssay("派克","A4纸");  
  18.     String s = essay.writeEssay("联想电脑");  
  19.     System.out.println(s);  
  20.       
  21.     }  
  22.   
  23. }  

5.Eclipse常用快捷键有哪些?

导包:ctrl+shift+O        

 代码格式化:ctrl+shift+F

代码提示:Alt+/                  注释:/**+Enter

注释代码:ctrl+/               

注意:有的快捷键被输入法占用

6.什么叫实参:调用方法的时候,传递的参数叫实参

7.什么是形参:定义方法的时候,括号里面的参数叫形参

8.定义方法的语法格式

【权限修饰符】方法返回值类型      方法名(形参列表)

{

    //由零条到多条可执行性语句组成的方法体

}

构造方法:是一种特殊的方法,其主要功能是用来在创建对象时初始化对象,即为对象成员变量赋初始值。

构造方法与类名相同,可以重载多个不同的构造方法。

构造方法没有返回类型,也不能定义为void,在方法名面前不能声明方法类型。


  1. public class Ex_animal {  
  2. public static void main(String[] args) {  
  3.         // TODO Auto-generated method stub  
  4.   
  5.         Animal an1 = new Animal("兔子","草",5);  
  6.         Animal an2 = new Animal();  
  7.         Animal an3 = new Animal(an1,an2);  
  8.         an2.all(an3);  
  9.     }  
  10. }  
  11. class Animal{  
  12.     String name;  
  13.     String eat;  
  14.     int weight;  
  15.     public Animal() {  
  16.         this.name = name;  
  17.         this.eat = "猪蹄";  
  18.         this.weight = 12;  
  19.     }  
  20.     public Animal(Animal a,Animal b) {  
  21.         this.name = b.name;  
  22.         this.eat = a.eat;  
  23.         this.weight = b.weight;  
  24.           
  25.     }  
  26.     public Animal(String name,String eat,int weight) {  
  27.         this.name = name;  
  28.         this.eat = eat;  
  29.         this.weight = weight;  
  30.     }  
  31.     public Animal(String name,int weight) {  
  32.         this.name = name;  
  33.         this.weight = weight;  
  34.     }  
  35.     public void eat1() {  
  36.         System.out.println("这个动物喜欢吃" + this.eat);  
  37.     }  
  38.     public void run() {  
  39.         System.out.println("这个动物会跑");  
  40.     }  
  41.     public void all(Animal an) {  
  42.         System.out.println("这是一只 " + this.name + ",它喜欢吃 " + an.eat + ",它重 " + an.weight +" 斤");  
  43.     }  
  44.   
  45.  }  
动物类2:
[java]  view plain  copy
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.   
  4.         Animal dog = new Animal("小黑""小猫""黑色");  
  5.         Animal cat = new Animal("小白""小狗""白色");  
  6.         String s = dog.play(cat, dog);  
  7.         System.out.println("获胜的动物是" + s);  
  8.     }  
  9. }  
  10.   
  11. class Animal {  
  12.     String name;  
  13.     String kind;  
  14.     int age;  
  15.     String color;  
  16.     long animalID;  
  17.     String date;  
  18.   
  19.     public Animal(String name, String kind) {  
  20.         this.name = name;  
  21.         this.kind = kind;  
  22.     }  
  23.   
  24.     public Animal(String name, String color, String kind) {  
  25.         this.name = name;  
  26.         this.color = color;  
  27.         this.kind = kind;  
  28.     }  
  29.   
  30.     public Animal(String name, int age, long animalID) {  
  31.         this.age = age;  
  32.         this.animalID = animalID;  
  33.     }  
  34.   
  35.     public String play(Animal dog, Animal cat) {  
  36.         System.out.println("这只" + dog.color + "是" + dog.name + "," + dog.kind + ",正在和那只" + cat.color + "叫做" + cat.name  
  37.                 + "," + cat.kind + ",在打架");  
  38.         return cat.kind;  
  39.     }  
  40. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值