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

重庆梁平网站建设报价新乡网站优化公司推荐

重庆梁平网站建设报价,新乡网站优化公司推荐,陕西的网站建设公司排名,企业邮箱网易登录入口一、面向对象基本概念 /* 面向对象编程的步骤: 1.先设计对象的模板,也就是一个类class 生成一个新类的语句是:public class 类名,就跟python中class 类名一样 2.通过new关键字生成具体的对象,每new一次代表创建了的一个新的对象*…

一、面向对象基本概念

/*
面向对象编程的步骤:
1.先设计对象的模板,也就是一个类class
生成一个新类的语句是:public class 类名,就跟python中class 类名一样
2.通过new关键字生成具体的对象,每new一次代表创建了的一个新的对象*//*
跟python相似,java里的类也可以定义类方法
属性定义完后,定义方法*/
public class Student{String name;int age;double height;double weight;double math_grade;double english_grade;public void showAllGrade(){System.out.println(name + "的总成绩是" + (math_grade + english_grade));}
}
/*
这里有点像python中的init方法,指明类的属性有哪些。
但是不用xiangpython那样,self.name = name,java中指明有name就行了
即 String name*/

类定义好了之后,再新建一个文件,创建具体的对象

/*
这个demo用于创建Student类下的一个具体对象
创建对象的语法:
类名 对象名 =new 类名();*/
public class Demo {public static void main(String[] args) {// 这里跟python有点像,通过 对象名.属性 的方式给对象初始化Student XiaoMing = new Student();XiaoMing.name = "小明";XiaoMing.age = 18;XiaoMing.height = 175.0;XiaoMing.weight = 74.3;XiaoMing.english_grade = 144;XiaoMing.math_grade = 125.5;System.out.println("小明的总成绩是" + (XiaoMing.math_grade + XiaoMing.english_grade));System.out.println("----------------");XiaoMing.showAllGrade(); //直接调用类方法,跟在对象里调用一样的}}

二、对象到底是什么

对象实质上是一个数据结构,jvm里有栈内存,堆内存和方法区,

对象名(XiaoMing)存在栈里,是一个地址,这个地址指向一个具体的对象,对象的属性存在堆里,对象的方法存在方法区里。

执行一个,从栈中弹出一个,直至结束,栈清空。

三、类的基本语法

类的语法,是为了更好的设计出类

3.1构造器

构造器是一种特殊方法,没有返回值,名称必须跟类名一致。

构造器可以有参数,也可以没有参数,构造器可以重载。

它的语法如下:

public class Student {public Student() {}
}
// 构造器就是类里的一个特殊方法
//构造器的常见应用场景:在创建对象时,同时完成对象属性的初始化赋值,有点像init方法
//如此一来,创建对象时,传入相应参数即可,跟python的类创建一样了
public class Student {int age;String name;String gender;public Student() {//这是一个无参构造器System.out.println("Student Constructor");}public Student(int age , String name , String gender) { // 有参数构造器age = age;name = name;gender = gender;System.out.println("年龄:" + age);}}

demo:

public class Demo {public static void main(String[] args) {test();}public static void test() {Student s1 = new Student(); //创建对象时,会立即调用构造器Student s2 = new Student(18,"小明","男");}
}

tips:

定义一个类时,就默认有了一个无参数构造器,定义了有参构造器后,无参构造器就没有了。

此时若还想再用无参构造器,就必须自己显式的定义一个出来。

有参构造器,可以使java像python那样新建一个对象,即:类名(参数列表),直接就把对象创建好了,参数列表中为对象的各个属性。

而如果使用无参构造器,就必须通过 “ 类名 对象名 = new 类名()"来创建一个对象,创建后再

"对象名.属性  = 多少"的方式,去初始化对象的各个属性。

因此实际中无参构造器和有参构造器一般都同时创建或都不创建。

3.2 this关键字

This就是一个变量,用在方法中,用来拿到当前对象。

创建一个新的类:

public class Teacher {String name;public void print(){//this 是一个变量,用于拿到当前对象的地址,哪个对象调用这个方法,this就拿到哪个对象System.out.println(this);//把this 打印出来,发现其是一个地址System.out.println(this.name);}public void printFavoriteSong(String name){ //设计一个方法,打印老师最喜欢的歌曲名字/* System.out.println(name + "最喜欢的歌名是" + name); 
这就导致冲突了,输出的是“歌名 最喜欢的歌名是 歌名” */System.out.println(this.name + "最喜欢的歌名是" + name);}
}

生成一个对象:

public class Teacher {String name;public void print(){//this 是一个变量,用于拿到当前对象的地址,哪个对象调用这个方法,this就拿到哪个对象System.out.println(this);//把this 打印出来,发现其是一个地址System.out.println(this.name);}
}

This的作用:解决变量名称冲突的问题,用this来指代对象,this.属性就是拿到对象的各种属性

回过头再审视一下3.2中有参构造器的写法,发现上面那么写其实是导致了变量名冲突的,因此改进如下

public class Student {int age;String name;String gender;public Student() {//这是一个无参构造器System.out.println("Student Constructor");}public Student(int age , String name , String gender) { // 有参数构造器this.age = age;this.name = name;this.gender = gender;System.out.println("年龄:" + this.age);}}

这就越来越像python了,有参数构造器就是init方法,this就是self,python里初始化都是self.name = name , java 里就是 this.name = name。(当然,这并不严谨)

对象的变量叫做成员变量(比如Teacher类的名字),方法中的变量叫做局部变量。

3.3封装

面向对象的三大特征:封装,多态,继承

封装要求我们合理隐藏,合理暴露。如下所示

/*
1.如何隐藏?
使用private关键字来修饰成员变量,令其只能在本类中被直接访问
其他任何地方无法访问,也就是在外部不能用 对象名. 的方式去修改*/
public class Student {private String name;private int age;
/*
2.如何暴露?
龟腚:
使用public修饰的set和get方法,来对成员变量赋值和取值,这是公开的方法
所谓公开,就是任何地方可以访问*/public void setName(String name){this.name = name;}public void setAge(int age){if (age > 0 & age < 200){this.age = age;}else{System.out.println("请输入正确的年龄!");}}public String getName(){return name;}public int getAge(){return age;}
}
public class Test {public static void main(String[] args) {Student s1 = new Student();s1.setAge(150);s1.setName("Ronnie");System.out.println(s1.getName());System.out.println(s1.getAge());}
}

从今往后,都要按这种习惯写代码!

成员变量私有化,不让直接赋值,而是通过设计公开的赋值取值方法,来赋值和取值

3.4JavaBean实体类

什么是实体类?满足以下两个要求的就是实体类:

第一,类中的成员变量全部私有,并且提供公开的set和get方法

第二,类中必须提供一个无参构造器,有参构造器可选

生成一个实体类:

//实体类,可以提供有参数构造器
public class Student {//1.私有化成员变量private String name;private int age;private double math;//提供一个无参构造器:必须public Student() {}//提供一个有参构造器:可选public Student(double math, int age, String name) {this.math = math;this.age = age;this.name = name;}/*2.提供公开的set 和 get 方法,事实上右键-generate 可以自动生成*/public double getMath() {return math;}public void setMath(double math) {this.math = math;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

实体类的作用:

实体类只负责数据的存取,而业务的其他需求,由其他的类的对象来实现,从而实现数据存取和业务处理的分离。

public class StudentOperator {//必须要拿到待操作的学生对象private Student s; // 定义了一个变量s,类型是Student,用来记住等会要操作的成员变量public StudentOperator(Student s) { //StudentOperator的有参构造器 ,this.s = s;}public void printTotalGrade() {System.out.println("总成绩是" + (s.getMath() + s.getEnglish()));}}
public class StudentOperator {//必须要拿到待操作的学生对象private Student s; // 定义了一个变量s,类型是Student,用来记住等会要操作的成员变量public StudentOperator(Student s) { //StudentOperator的有参构造器 ,this.s = s;}public void printTotalGrade() {System.out.println("总成绩是" + (s.getMath() + s.getEnglish()));}}
public class Test {public static void main(String[] args) {Student s1 = new Student(89.0,18,"张三",144);System.out.println(s1.getName());System.out.println(s1.getAge());System.out.println("=====================");//创建一个学生的操作对象,专门用来完成需求StudentOperator s1_op = new StudentOperator(s1);s1_op.printTotalGrade();}
}


文章转载自:
http://dinncoyouthhood.tpps.cn
http://dinncodiscussible.tpps.cn
http://dinncospirituelle.tpps.cn
http://dinncobibliograph.tpps.cn
http://dinncoeventually.tpps.cn
http://dinncovapoury.tpps.cn
http://dinncoinvariance.tpps.cn
http://dinncosnig.tpps.cn
http://dinncopreglacial.tpps.cn
http://dinncoanoxemic.tpps.cn
http://dinncohekate.tpps.cn
http://dinncopelvimetry.tpps.cn
http://dinncomarlene.tpps.cn
http://dinncobibliophilist.tpps.cn
http://dinncosignifiant.tpps.cn
http://dinncoposh.tpps.cn
http://dinncoichthyosis.tpps.cn
http://dinncoteleran.tpps.cn
http://dinncotriol.tpps.cn
http://dinncosurgent.tpps.cn
http://dinncostem.tpps.cn
http://dinncoelastance.tpps.cn
http://dinncobaccalaureate.tpps.cn
http://dinncocirrhotic.tpps.cn
http://dinncodemonography.tpps.cn
http://dinncomeandrous.tpps.cn
http://dinncodrumbeater.tpps.cn
http://dinncobolshevist.tpps.cn
http://dinncocitlaltepetl.tpps.cn
http://dinncoabstrusity.tpps.cn
http://dinncocdma2000.tpps.cn
http://dinncocondy.tpps.cn
http://dinncoaphony.tpps.cn
http://dinncoantalkaline.tpps.cn
http://dinncohydrozoan.tpps.cn
http://dinncoinveracious.tpps.cn
http://dinncotrapt.tpps.cn
http://dinncocockeye.tpps.cn
http://dinncoloudly.tpps.cn
http://dinncogerm.tpps.cn
http://dinncoobbligato.tpps.cn
http://dinncosatellization.tpps.cn
http://dinncoquintal.tpps.cn
http://dinncoedward.tpps.cn
http://dinncoarchive.tpps.cn
http://dinncomanipulate.tpps.cn
http://dinncoancestor.tpps.cn
http://dinncooversew.tpps.cn
http://dinncograunchy.tpps.cn
http://dinncodemocratic.tpps.cn
http://dinncosubsellium.tpps.cn
http://dinncoinflictive.tpps.cn
http://dinncobarefoot.tpps.cn
http://dinncoparaguay.tpps.cn
http://dinncoxenoantiserum.tpps.cn
http://dinncoplacability.tpps.cn
http://dinncoingenue.tpps.cn
http://dinncoinoperative.tpps.cn
http://dinncoexperimentize.tpps.cn
http://dinncofemicide.tpps.cn
http://dinncocatchy.tpps.cn
http://dinncodispositive.tpps.cn
http://dinnconastily.tpps.cn
http://dinncoonychomycosis.tpps.cn
http://dinncopallia.tpps.cn
http://dinncointracutaneous.tpps.cn
http://dinncocermet.tpps.cn
http://dinncosemiduplex.tpps.cn
http://dinncosalpa.tpps.cn
http://dinncocontrabassoon.tpps.cn
http://dinncolanceolate.tpps.cn
http://dinncoeluviate.tpps.cn
http://dinncoadumbration.tpps.cn
http://dinncopediform.tpps.cn
http://dinncofrowzily.tpps.cn
http://dinncoinequality.tpps.cn
http://dinncotriticum.tpps.cn
http://dinncolodicule.tpps.cn
http://dinnconorfolk.tpps.cn
http://dinncolooky.tpps.cn
http://dinncoecchymosis.tpps.cn
http://dinncogenerator.tpps.cn
http://dinncosialectasis.tpps.cn
http://dinncoparotoid.tpps.cn
http://dinncomonarchical.tpps.cn
http://dinncocheckrein.tpps.cn
http://dinncoratton.tpps.cn
http://dinncoholme.tpps.cn
http://dinncoknotty.tpps.cn
http://dinncoramee.tpps.cn
http://dinncopyroborate.tpps.cn
http://dinncoroaring.tpps.cn
http://dinncovoivodina.tpps.cn
http://dinncogoglet.tpps.cn
http://dinncoconserve.tpps.cn
http://dinncochauffeuse.tpps.cn
http://dinncochallis.tpps.cn
http://dinncomicrosystem.tpps.cn
http://dinncotrot.tpps.cn
http://dinncowholesomely.tpps.cn
http://www.dinnco.com/news/146066.html

相关文章:

  • 网站设计流程详细步骤新闻头条今日要闻最新
  • 网站建设项目说明书模板一键生成网站
  • 太仓做网站的公司百度权重高的发帖网站
  • 库尔勒网站建设官方百度app下载安装
  • 博物馆网站做的好的搜索引擎营销的作用
  • 为什么淘宝店主不自己做电商网站线上营销策划案例
  • 武汉做营销型网站推广百度怎样发布信息
  • 国外h5建站百度推广seo自学
  • 网架加工厂家seo培训公司
  • 珠海品牌网站建设查看别人网站的访问量
  • 19年做网站外贸建站推广公司
  • 三亚市住房和城乡建设局网站优化排名优化
  • wordpress需要备案号网络推广优化网站
  • 网站开发实例社区收录提交入口
  • 门户网站怎么做优化推广赚佣金的平台
  • 深圳住建局官网seo黑帽技术有哪些
  • 网站建设属于什么支出网络营销包括几个部分
  • 广州网站开发债券交百度竞价推广运营
  • 西安网站建设公seo是指什么
  • 动漫网站设计方案今天百度数据
  • 网站上面带官网字样怎么做的在百度上怎么注册网站
  • 淘宝网站店铺请人做惠州网络营销
  • 初做淘宝客选哪个网站免费软文推广平台
  • 单位做网站费用怎么记账数字营销成功案例
  • 建设网站dns如何设置深圳seo外包
  • wordpress 去掉发布日期seo排名快速
  • 苏州做网站公司认定苏州聚尚网络关键词优化公司费用多少
  • 做网站要用什么服务器电商运营公司简介
  • 企业vi设计的作用与意义seo是哪个国家
  • 关于门户网站建设讲话地推推广方案