Java语言基础(11)
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)的更多相关文章
- Java之--Java语言基础组成(关键字、标识符、注释、常量和变量、运算符)
Java语言基础组成-关键字.标识符.注释.常量和变量.运算符 Java语言由8个模块构成,分别为:1.关键字:2.标识符(包名.类名.接口名.常量名.变量名等):3.注释:4.常量和变量:5.运算符 ...
- Java语言基础(五)
Java语言基础(五) 一.浮点数 浮点数就是小数,其标准是IEEE 754,用指数和尾数表示 例如30000=3*10000=3*10^4 其中4是指数,3是尾数 Java中,浮点数有float ...
- Java入门篇(二)——Java语言基础(下)
上篇说到Java中的变量与常量,接下来就是简单的计算了,首先需要了解一下Java中的运算符. 六.运算符 1. 赋值运算符 赋值运算符即"=",是一个二元运算符(即对两个操作数进行 ...
- JAVA语言基础内部测试题(50道选择题)
JAVA语言基础内部测试题 选择题(针对以下题目,请选择最符合题目要求的答案,针对每一道题目,所有答案都选对,则该题得分,所选答案错误或不能选出所有答案,则该题不得分.)(每题2分) 没有注明选择几项 ...
- day05<Java语言基础--数组>
Java语言基础(数组概述和定义格式说明) Java语言基础(数组的初始化动态初始化) Java语言基础(Java中的内存分配以及栈和堆的区别) Java语言基础(数组的内存图解1一个数组) Java ...
- day04<Java语言基础+>
Java语言基础(循环结构概述和for语句的格式及其使用) Java语言基础(循环结构for语句的练习之获取数据) Java语言基础(循环结构for语句的练习之求和思想) Java语言基础(循环结构f ...
- day03<Java语言基础+>
Java语言基础(逻辑运算符的基本用法) Java语言基础(逻辑运算符&&和&的区别) Java语言基础(位运算符的基本用法1) Java语言基础(位异或运算符的特点及面试题) ...
- day02<Java语言基础+>
Java语言基础(常量的概述和使用) Java语言基础(进制概述和二,八,十六进制图解) Java语言基础(不同进制数据的表现形式) Java语言基础(任意进制到十进制的转换图解) Java语言基础( ...
- 2018.6.13 Java语言基础复习总结
Java语言基础与面向对象编程实践 第一章 初识Java 1.1机器语言 机器语言是指一台计算机全部的指令集合.机器语言室友0和1组成的二进制数,是一串串由0和1组成的指令序列,可将这些指令序列交给计 ...
随机推荐
- JMeter—压力测试&性能测试工具
安装 下载 官方网站下载最新版本: http://jmeter.apache.org/download_jmeter.cgi,使用JMeter依赖jdk,建议安装jdk 1.6版本以上. 环境变量配置 ...
- 用myeclipse连接MySQL8.0时没有配置jar包
先上测试代码 package testJdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql. ...
- C学习笔记-字符串处理函数
字符串函数是最问常用的库函数之一,本文整理了常用的字符串函数,其来源为互联网 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, ...
- NIKKEI Programming Contest 2019-2 Task D. Shortest Path on a Line
Observations ① 从 $1$ 到 $N$ 的最短路一定是不走回头路的.所谓走回头路是指从序号大的点走到序号小的点. 证明:首先,任意从 $1$ 到 $N$ 的路径的最后一步一定不是回头路. ...
- LINK : fatal error LNK1104: cannot open file的解决方法
结果是编译时通过了,但连接(F7)时却显示: LINK : fatal error LNK1104: cannot open file“Debug/1.exe” ============== 上一次运 ...
- Elastic Search中mapping的问题
Mapping在ES中是非常重要的一个概念.决定了一个index中的field使用什么数据格式存储,使用什么分词器解析,是否有子字段,是否需要copy to其他字段等.Mapping决定了index中 ...
- how to Simply Singleton Navigate the deceptively simple Singleton pattern---reference
http://www.javaworld.com/article/2073352/core-java/simply-singleton.html JAVA DESIGN PATTERNS By Dav ...
- hdu 4632区间dp 回文字串计数问题
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- 进阶Java编程(11)ClassLoader类加载器【待完成】
1,ClassLoader类加载器简介 在Java里面提供一个系统的环境变量:ClassPath,这个属性的作用主要是在JVM进程启动的时候进行类加载路径的定义,在JVM里面可以根据类加载器而后进行指 ...
- url协议+域名+端口号
string url = System.Web.HttpContext.Current.Request.Url.Scheme + "://" + ...