• Вход
  • Быстрая регистрация
Перейти на сайт
так английский ты еще не учил!
Регистрация Вход
  • Материалы
  • Курсы
  • Словарь

Programming Methodology (Stanford). Lecture 10.

Материал готовится,
пожалуйста, возвращайтесь позднее
So with that said, are there any questions about classes right now, just [inaudible] anything we’ve done?

I wanna spend a little bit of time touching on classes before we dive into our next great topic today, which is graphics. So if we actually have the computer for a second, I just wanna briefly review the classes that we wrote last time.

So we wrote a class that hopefully is near and dear to many of you because many of you are instances of this class, which is a student. And we went through, and we talked about all the things that you actually wanna have in a class. And hopefully this is just review.

You have some constructors in the class. You can have multiple different constructors in the class. But here, we just have one where we take [inaudible] a name and ID, and that name and ID gets set to some instance variables or ibars that all students have, namely student name and student ID.

So I’ll scroll down to the bottom, and here we have our private instance variables, student names, student ID, and units earned. These are the variables that every student’s object has their own copy of. That’s why they’re instance variables as opposed to, say, a class variable.

This guy over here, because it’s got a static, is called a class variable because it’s got a final that’s actually a constant, as hopefully, you’ve seen many times by now. And so all units – all students share units to graduate as the same constant value, 180.

So it’s both a constant and a class variable. Most things that are constants make sense to be class variables because all elements of that class sort of share the same constant value. All students require 180 units to graduate.

So we did a bunch of things in here, and we kinda went through the constructor. We went through a few places where we said, “Hey, if you wanna be able to access portions of this class which are private, you can’t directly access them from outside.” That’s why they’re private. So no one can touch your private parts. And so what you needed to have was you needed to have these functions if you wanted to allow people to access them, that we refer to as getters.

And the reason why we refer to them as getters is because they start with the name “get.” And what they do is they’re public methods. So someone can actually call these methods from outside of your class. And they just return the value for some appropriate thing that the person would want. GetID would return student ID.

Now, you might say that’s kinda weird. Why do I have these getters when student ID, I could just make public and let people access it directly? Why would I do this?

Student: [Inaudible]

Instructor (Mehran Sahami): So you can’t change it. If student ID was public, that’s a public variable. People cannot only read what the value is; they can set what the value is. If I make it private, I control access to it, and here, I could let you read what the idea is by giving you a copy of the ID. I don’t let you set it. The only way the ID actually gets set is when a student first gets created, and you get a student ID up here, which you have for life. So when you get created, you have the ID for life. This is actually true at Stanford.

As a faculty member, now, I have the same ID number, no joke, that I had when I was a student. It’s kinda funky, and it’s just like, “You are in our universal system, and you will be here for the rest of your life.
Загрузка...
Выбрать следующее задание
Ты добавил
Выбрать следующее задание
Ты добавил
Лео-переводчик
Мобильные приложения
FAQ Поддержка Вакансии Блог Сотрудничество