课程: Java Object-Oriented Programming
今天就学习课程吧!
今天就开通帐号,24,600 门业界名师课程任您挑!
Building objects with a constructor
- [Instructor] Classes only represent a general blueprint but they become more tangible when you use a constructor to bring your blueprint to life. To create trees from our tree class we'll need to add a special type of function called a constructor to construct our tree objects. To create a constructor, we'll use the class name tree. We don't need a return type because the name of the function, tree, counts as the return type as well, since tree is the class name. This classifies it as a constructor. To build a tree we'll need to know what its height, diameter and tree type should be. This means we'll need to add inputs to the constructor so that we can construct a custom tree with the appropriate height, diameter and type. Of course, the body of the function doesn't do anything yet. We'll need to set up the tree we're building with the appropriate values. Using the, this keyword, we access the tree we're in the midst of creating, and set the height, trunk and type on the left equal…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
(已锁定)
Using classes as blueprints4 分钟
-
(已锁定)
Building objects with a constructor2 分钟 48 秒
-
(已锁定)
Using concrete instances for application logic4 分钟 32 秒
-
(已锁定)
Understanding the difference between class and instance members4 分钟 54 秒
-
(已锁定)
Exploring a built-in Java class4 分钟 5 秒
-
(已锁定)
Challenge: Build an employee class1 分钟 8 秒
-
(已锁定)
Solution: Build an employee class3 分钟 11 秒
-
(已锁定)
-
-
-
-
-