As for the military, Mr. Trump’s comments suggested that he saw service members as another constituency: Like factory workers, farmers and coal miners, they seemed to be cast as an interest group to be wooed. MacDill, Mr. Trump said, was “quite a place, and we’re going to be loading it up with beautiful new planes and beautiful new equipment.”

Mr. Trump’s overt partisanship before an audience of armed forces personnel runs against a decades-long legacy of civilians who have guided the military in keeping with a president’s policies, while maintaining a distance from overt presidential politics.

“Many presidents pander to the military and through it to voters who focus on national defense,” said Richard H. Kohn, an expert on civil-military relations and professor emeritus at the University of North Carolina. “But leading off with the election, attacking the press and talking about endorsements is a clear attempt to politicize the military and invite their partisanship. In rhetoric and style, his words mimicked a campaign rally.”

George C. Marshall, the five-star general who also led the State Department and the Pentagon, once observed that he was so determined to avoid the appearance of being political that he did not vote. That legacy has continued, and is honored by a number of military officers who have pinned stars on their shoulders.

During the election season, there was considerable debate about whether it was appropriate for retired generals like Michael T. Flynn (for Mr. Trump) or John R. Allen (for Hillary Clinton) to endorse candidates. But there was no question that active-duty officers were to stay far away from politics.

Gen. David L. Goldfein, the Air Force chief of staff, told reporters on Tuesday that military leaders had an obligation to “speak truth to power regardless of party.” He declined to comment on Mr. Trump’s remarks.

Peter D. Feaver, a political science professor at Duke University who served on the staff of the National Security Council under Presidents George W. Bush and Bill Clinton, said that Mr. Trump was right to try to build a relationship with the military he now commands. But he said it was a mistake for the president to speculate about its voting behavior.