1 构造方法

  构造方法是一种特殊的方法,只有在创建对象的时候才被调用,用来执行初始化的操作,比如给属性赋值...

  1) 构造方法名字跟类名一致,没有返回值也就没有返回值类型
  2) 格式:
    类名(参数列表){
      方法体
    }
  3) 创建对象的标准格式:
    类名 对象名 = new 构造方法
    案例:Demo1

public class Demo11 {
public static void main(String[] args) {
Math2 math = new Math2();
math.sub(100, 50);
math.sub(100.1, 2.3);
math.sub(2.99, 1);
math.sub(100.1, 100);
}
}
class Math2{
void sub(int num1,int num2){
int result = num1 - num2;
System.out.println("result = "+result);
}
void sub(double num1,int num2){
double result = num1 - num2;
System.out.println("result = "+result);
}
void sub(int num1,double num2){
double result = num1 - num2;
System.out.println("result = "+result);
}
void sub(double num1,double num2){
double result = num1 - num2;
System.out.println("result = "+result);
}
} public class Demo2 {
public static void main(String[] args) {
User user1 =
new User("刘备",42,"abc","成都",
"bei@126.com");
user1.show();
User user2 =
new User("黄忠",56,"123456","襄阳",
"zhong@163.com");
user2.show();
}
}
class User{
//属性
String name;
int age;
String password;
String address;
String email;
//构造方法
User(String name1,int age1,
String password1,String address1,
String email1){
name = name1;
age = age1;
password = password1;
address = address1;
email = email1;
}
//方法
void show(){
System.out.println(name+","+age+","
+password+","+address+","+
email);
}
}

  4) 如果该类中不编写构造方法,则系统默认提供一个空参的构造方法;反之如果编写了构造方法,则会覆盖系统默认提供的空参的构造方法。
    案例:Demo3

public class Demo3 {
public static void main(String[] args) {
//类名 对象名 = new 构造方法
Foo foo = new Foo();
}
}
//如果该类中没有编写构造方法,则系统默认提供一个空参
//构造方法;如果该类中编写了构造方法,则会覆盖系统
//默认提供的空参的构造方法。
class Foo{
//Foo(){系统默认提供空参(没有参数)的构造方法
//}
} public class Demo4 {
public static void main(String[] args) {
// Koo koo = new Koo();//默认空参构造方法
//报错,在Koo的内部又编写一个带两个参数的构造方法,
//会覆盖系统提供的空参的构造方法。在Koo内部只有编写
//的带两个参数的构造方法。
Koo koo2 = new Koo("张三",21); }
}
class Koo{
String name;
int age;
Koo(String name1,int age1){
name = name1;
age = age1;
}
} public class Demo5 {
public static void main(String[] args) {
Box1 box = new Box1(100,20,90);
box.show();
box.showTiji();
Box1 box2 = new Box1(200,100,150);
box2.show();
box2.showTiji();
}
}
class Box1{
//属性
int length;
int width;
int height;
//构造方法
Box1(int length1,int width1,int height1){
length = length1;
width = width1;
height = height1;
}
//方法
void show(){
System.out.println(length+","+width
+","+height);
}
void showTiji(){
int result = length * width * height;
System.out.println("体积是"+result);
}
}

  5) 在企业中建议,构造方法中参数的名字跟要赋值的属性名最好一致,这样对应关系更加的清楚。
    this.属性名:指代当前的属性
    class Person{
      //属性
      String name;
      int age;
      double salary;
      String address;
      //构造方法
      Person(String name,int age,double salary,
      String address){
        this.name = name;
        // 属性 参数
        this.age = age;
        this.salary = salary;
        this.address = address;
      }
    }
    案例:Demo6

public class Demo6 {
public static void main(String[] args) {
Person2 p =
new Person2("李白",31,20000.1);
p.show();
}
}
class Person2{
//属性:
String name;
int age;
double salary;
//构造方法:
Person2(String name,int age,
double salary){
this.name = name;
this.age = age;
this.salary = salary;
// 属性 参数
}
//方法:void show():输出每一个属性值
void show(){
System.out.println(name+","+age+","
+salary);
}
} public class Demo7 {
public static void main(String[] args) {
User2 user =
new User2("张三丰","abc",
99,"道士","13555555555");
user.showAll();
}
}
class User2{
//属性
String name;
String password;
int age;
String job;
String phone;
//构造方法
User2(String name,String password,
int age,String job,String phone){
this.name = name;
this.password = password;
this.age = age;
this.job = job;
this.phone = phone;
}
//方法
void showAll(){
System.out.println(name+","+password
+","+age+","+job+","+phone);
}
} public class Demo8 {
public static void main(String[] args) {
Manager manager =
new Manager("张居正",32,
2000000.1,1000000.1,"北京");
manager.show();
manager.showYearSal();
}
}
class Manager{
//属性
String name;
int age;
double salary;
double comm;
String address;
//构造方法
Manager(String name,int age,
double salary,double comm,
String address){
this.name = name;
this.age = age;
this.salary = salary;
this.comm = comm;
this.address = address;
}
//方法
void show(){
System.out.println(name+","+age+","
+salary+","+comm+","+address);
}
void showYearSal(){
double result = salary*12+comm;
System.out.println(name+"的年薪是"+
result);
}
} public class Demo9 {
public static void main(String[] args) {
Car car =
new Car("哥的奥迪","666666","内敛奢华",
"黑色",2000000000.1);
car.show();
car.start();
car.run();
car.stop();
}
}
class Car{
//属性
String name;
String no;
String type;
String color;
double price;
int speed=0;
//构造方法
Car(String name,String no,String type,
String color,double price){
this.name = name;
this.no = no;
this.type = type;
this.color = color;
this.price = price;
}
//方法
void show(){
System.out.println(name+","+no+","+
type+","+color+","+price+
","+speed);
}
void start(){
speed = 100;
System.out.println(name+"启动了");
}
void run(){
System.out.println(name+"在行驶," +
"当前的速度是"+speed);
}
void stop(){
speed = 0;
System.out.println(name+"刹车了," +
"当前的速度是"+speed);
}
}

    总结:构造方法是一种特殊的方法,只有在创建对象的时候才被调用,用来执行初始化的操作,比如给属性赋值...构造方法的名字跟类名一致,没有返回值类型。

2 方法重载

  在一个类的内部,方法名相同,参数不同的多个方法。
  案例:Demo10

public class Demo10 {
public static void main(String[] args) {
/*
Math1 math = new Math1();
math.add3(1.1, 200);
math.add4(2.2,5.3);
math.add1(300, 500);
math.add2(1000, 2.2);
*/
Math1 math = new Math1();
math.add(100, 100);
math.add(200, 1.66);
math.add(10.1, 1000);
math.add(2.6, 3.3);
}
}
class Math1{
void add(int num1,int num2){
int sum = num1 + num2;
System.out.println(sum);
}
void add(int num1,double num2){
double sum = num1 + num2;
System.out.println(sum);
}
void add(double num1,int num2){
double sum = num1 + num2;
System.out.println(sum);
}
void add(double num1,double num2){
double sum = num1 + num2;
System.out.println(sum);
}
} public class Demo11 {
public static void main(String[] args) {
Math2 math = new Math2();
math.sub(100, 50);
math.sub(100.1, 2.3);
math.sub(2.99, 1);
math.sub(100.1, 100);
}
}
class Math2{
void sub(int num1,int num2){
int result = num1 - num2;
System.out.println("result = "+result);
}
void sub(double num1,int num2){
double result = num1 - num2;
System.out.println("result = "+result);
}
void sub(int num1,double num2){
double result = num1 - num2;
System.out.println("result = "+result);
}
void sub(double num1,double num2){
double result = num1 - num2;
System.out.println("result = "+result);
}
}

  1)编写类的方法时候,可以把功能相似的方法起同一个名字,参数要不同,通过参数区分开每一个方法,用户只需要记住一个名字,就可以调用相似功能的多个方法。
  2)参数不同:参数类型或者个数必须有一个不同,跟形参的名字无关。
  3)方法重载的好处:方法重载并没有简化代码的编写,但是用户只需要记住一个名字,就可以调用相似功能的多个方法,简少用户的记忆量,方便用户调用方法。
  4)方法重载属于多态的一种。
    多态:多态是面向对象的编程思想之一,事物在不同的情况下有不同的表现。
    eg:花朵,花生,花心,花钱
  5)Sun公司编写的工具类,经常使用到方法重载,方便用户来访问这些方法,用户只要记住一个名字,就可以调用相似功能的多个方法。
    System.out.println(100);
    System.out.println(1.66);
    System.out.println(false);
    System.out.println('中');
    System.out.println("今天风很大");
    案例:Demo12

public class Demo12 {
public static void main(String[] args) {
Kongtiao kt =
new Kongtiao("美的","柜式",
20000.1,"白色",25);
kt.show();
kt.cool();
kt.cool(5);
kt.hot();
kt.hot(3);
}
}
class Kongtiao{
//属性
String name;
String type;
double price;
String color;
int degree;
//构造方法
Kongtiao(String name,String type,
double price,String color,
int degree){
this.name = name;
this.type = type;
this.price = price;
this.color = color;
this.degree = degree;
}
//方法
void show(){
System.out.println(name+","+type+","
+price+","+color+","+degree);
}
void hot(){
degree++;
System.out.println(name+"升高1度以后,"
+"当前的温度是"+degree);
}
void hot(int degree){
this.degree = this.degree + degree;
System.out.println(name+"升高"+degree
+"度以后,当前的温度"+this.degree);
}
void cool(){
degree--;
System.out.println(name+"降低1度以后,"
+"当前的温度是"+degree);
}
void cool(int degree){
this.degree = this.degree - degree;
System.out.println(name+"降低"+degree
+"度以后,当前的温度是"+this.degree);
}
} public class Demo13 {
public static void main(String[] args) {
Zoo1 zoo = new Zoo1();
zoo.f1();
zoo.f2(10);
}
}
//如果一个方法中的参数名字跟属性一致,在该方法中,
//必须使用this.属性名,来指代当前的属性,如果不加
//this关键字,就是参数
class Zoo1{
//属性
int num1 = 100;
//方法
void f1(){
System.out.println("属性num1的值是"
+num1);
}
void f2(int num1){
num1 = 1000;//参数
System.out.println("参数num1的值是"
+num1);
//this.属性名:指代当前的属性
System.out.println("属性num1的值是"
+this.num1);
}
}

Java语言基础(11)的更多相关文章

  1. Java之--Java语言基础组成(关键字、标识符、注释、常量和变量、运算符)

    Java语言基础组成-关键字.标识符.注释.常量和变量.运算符 Java语言由8个模块构成,分别为:1.关键字:2.标识符(包名.类名.接口名.常量名.变量名等):3.注释:4.常量和变量:5.运算符 ...

  2. Java语言基础(五)

    Java语言基础(五) 一.浮点数 浮点数就是小数,其标准是IEEE 754,用指数和尾数表示 例如30000=3*10000=3*10^4  其中4是指数,3是尾数 Java中,浮点数有float ...

  3. Java入门篇(二)——Java语言基础(下)

    上篇说到Java中的变量与常量,接下来就是简单的计算了,首先需要了解一下Java中的运算符. 六.运算符 1. 赋值运算符 赋值运算符即"=",是一个二元运算符(即对两个操作数进行 ...

  4. JAVA语言基础内部测试题(50道选择题)

    JAVA语言基础内部测试题 选择题(针对以下题目,请选择最符合题目要求的答案,针对每一道题目,所有答案都选对,则该题得分,所选答案错误或不能选出所有答案,则该题不得分.)(每题2分) 没有注明选择几项 ...

  5. day05<Java语言基础--数组>

    Java语言基础(数组概述和定义格式说明) Java语言基础(数组的初始化动态初始化) Java语言基础(Java中的内存分配以及栈和堆的区别) Java语言基础(数组的内存图解1一个数组) Java ...

  6. day04<Java语言基础+>

    Java语言基础(循环结构概述和for语句的格式及其使用) Java语言基础(循环结构for语句的练习之获取数据) Java语言基础(循环结构for语句的练习之求和思想) Java语言基础(循环结构f ...

  7. day03<Java语言基础+>

    Java语言基础(逻辑运算符的基本用法) Java语言基础(逻辑运算符&&和&的区别) Java语言基础(位运算符的基本用法1) Java语言基础(位异或运算符的特点及面试题) ...

  8. day02<Java语言基础+>

    Java语言基础(常量的概述和使用) Java语言基础(进制概述和二,八,十六进制图解) Java语言基础(不同进制数据的表现形式) Java语言基础(任意进制到十进制的转换图解) Java语言基础( ...

  9. 2018.6.13 Java语言基础复习总结

    Java语言基础与面向对象编程实践 第一章 初识Java 1.1机器语言 机器语言是指一台计算机全部的指令集合.机器语言室友0和1组成的二进制数,是一串串由0和1组成的指令序列,可将这些指令序列交给计 ...

随机推荐

  1. 【JAVA系列】使用JavaScript实现网站访问次数统计代码

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[JAVA系列]使用JavaScript实现网站 ...

  2. pacman 命令(安装所有软件包)

    MSYS2 中pacman安装所有软件包命令: pacman -Sl | awk '{cmd="pacman -S --needed --noconfirm "$2;system( ...

  3. K8S知识点总结

    一.K8S介绍: Kubernetes(k8s)是Google开源的容器集群管理系统.在Docker技术的基础上,为容器化的应用提供部署运行.资源调度.服务发现和动态伸缩等一系列完整功能,提高了大规模 ...

  4. 记录运行时间 StopWatch

  5. 【并行计算-CUDA开发】CUDA并行存储模型

    CUDA并行存储模型 CUDA将CPU作为主机(Host),GPU作为设备(Device).一个系统中可以有一个主机和多个设备.CPU负责逻辑性强的事务处理和串行计算,GPU专注于执行高度线程化的并行 ...

  6. PTA (Advanced Level)1035.Password

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  7. ARC100E. Or Plus Max

    题目 好题.没想出解法. 官方题解: 这个解法和 Small Multiple 那道题的解法有异曲同工之妙. 扩展 若把 $\mathsf{or}$ 改成 $\mathsf{and}$ 或者 $\ma ...

  8. Tomcat中的Host和Engine级别的servlet容器

    这边文章主要介绍的是Host容器 和 Engine容器.如果你想在同一个Tomcat上部署运行多个Context容器的话,你就需要使用Host容器,从理论上来讲,如果你的Tomcat只想要部署一个Co ...

  9. [js]$.ajax标准写法

    $.ajax({     url:"http://www.microsoft.com",    //请求的url地址     dataType:"json",  ...

  10. JS基础_基本数据类型和引用数据类型

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...